blob: 964b7f3fff6d3e37f04fb38e6015c3a1ad99cb28 [file] [log] [blame]
Andreas Gampee1459ae2016-06-29 09:36:30 -07001/*
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
David Sehr891a50e2017-10-27 17:01:07 -070026#include "base/file_utils.h"
David Sehrc431b9d2018-03-02 12:01:51 -080027#include "base/os.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070028#include "base/stl_util.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/utils.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070030#include "common_runtime_test.h"
31#include "compiler_callbacks.h"
David Sehr013fd802018-01-11 22:55:24 -080032#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file_loader.h"
David Sehr97c381e2017-02-01 15:09:58 -080034#include "exec_utils.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070035#include "gc/heap.h"
36#include "gc/space/image_space.h"
37#include "oat_file_assistant.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070038#include "runtime.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070039
40namespace art {
41
Calin Juravle858cfd82020-11-23 19:21:09 -080042static constexpr bool kDebugArgs = false;
43
Andreas Gampee1459ae2016-06-29 09:36:30 -070044// Test class that provides some helpers to set a test up for compilation using dex2oat.
45class Dex2oatEnvironmentTest : public CommonRuntimeTest {
46 public:
Roland Levillainf73caca2018-08-24 17:19:07 +010047 void SetUp() override {
Andreas Gampee1459ae2016-06-29 09:36:30 -070048 CommonRuntimeTest::SetUp();
David Sehr013fd802018-01-11 22:55:24 -080049 const ArtDexFileLoader dex_file_loader;
Andreas Gampee1459ae2016-06-29 09:36:30 -070050
51 // Create a scratch directory to work from.
Calin Juravle357c66d2017-05-04 01:57:17 +000052
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";
Andreas Gampee1459ae2016-06-29 09:36:30 -070062 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
Richard Uhler84f50ae2017-02-06 15:12:45 +000072 std::vector<uint32_t> checksums;
Calin Juravle6e6f1b22020-12-15 19:13:19 -080073 std::vector<std::string> dex_locations;
Andreas Gampee1459ae2016-06-29 09:36:30 -070074 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();
Calin Juravle5ff23932020-12-11 18:26:14 -080079 ASSERT_TRUE(OS::FileExists(GetResourceOnlySrc1().c_str()))
80 << "Expected stripped dex file to be at: " << GetResourceOnlySrc1();
Mathieu Chartier79c87da2017-10-10 11:54:29 -070081 ASSERT_FALSE(
Calin Juravle6e6f1b22020-12-15 19:13:19 -080082 dex_file_loader.GetMultiDexChecksums(
83 GetResourceOnlySrc1().c_str(), &checksums, &dex_locations, &error_msg))
Calin Juravle5ff23932020-12-11 18:26:14 -080084 << "Expected stripped dex file to be stripped: " << GetResourceOnlySrc1();
Andreas Gampee1459ae2016-06-29 09:36:30 -070085 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;
David Sehr013fd802018-01-11 22:55:24 -080092 ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc1().c_str(),
93 GetMultiDexSrc1().c_str(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -070094 /* verify= */ true,
David Sehr013fd802018-01-11 22:55:24 -080095 kVerifyChecksum,
96 &error_msg,
97 &multi1)) << error_msg;
Andreas Gampee1459ae2016-06-29 09:36:30 -070098 ASSERT_GT(multi1.size(), 1u);
99
100 std::vector<std::unique_ptr<const DexFile>> multi2;
David Sehr013fd802018-01-11 22:55:24 -0800101 ASSERT_TRUE(dex_file_loader.Open(GetMultiDexSrc2().c_str(),
102 GetMultiDexSrc2().c_str(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700103 /* verify= */ true,
David Sehr013fd802018-01-11 22:55:24 -0800104 kVerifyChecksum,
105 &error_msg,
106 &multi2)) << error_msg;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700107 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
Roland Levillainf73caca2018-08-24 17:19:07 +0100113 void SetUpRuntimeOptions(RuntimeOptions* options) override {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700114 // 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
Roland Levillainf73caca2018-08-24 17:19:07 +0100124 void TearDown() override {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700125 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
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
Andreas Gampee1459ae2016-06-29 09:36:30 -0700144 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.
Calin Juravle5ff23932020-12-11 18:26:14 -0800150 std::string GetResourceOnlySrc1() const {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700151 return GetTestDexFileName("MainStripped");
152 }
153
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.
160 std::string GetMultiDexSrc2() const {
161 return GetTestDexFileName("MultiDexModifiedSecondary");
162 }
163
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).
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.
176 const std::string& GetOdexDir() const {
177 return odex_dir_;
178 }
179
Calin Juravle858cfd82020-11-23 19:21:09 -0800180 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
Andreas Gampee1459ae2016-06-29 09:36:30 -0700236 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_