blob: eb5b149039cb89ae3be0eeccfb43737fc31528e7 [file] [log] [blame]
Calin Juravle36eb3132017-01-13 16:32:38 -08001/*
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 <string>
18#include <vector>
19
Calin Juravle36eb3132017-01-13 16:32:38 -080020#include <gtest/gtest.h>
Christopher Ferris71884782022-03-26 15:37:43 -070021#include <procinfo/process_map.h>
Calin Juravle36eb3132017-01-13 16:32:38 -080022
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000023#include "android-base/stringprintf.h"
24#include "android-base/strings.h"
Chris Morin88c6d262018-02-13 15:26:21 -080025#include "base/file_utils.h"
David Sehr79e26072018-04-06 17:58:50 -070026#include "base/mem_map.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080027#include "common_runtime_test.h"
28#include "compiler_callbacks.h"
Calin Juravle028c7ef2021-05-26 15:37:00 -070029#include "dex/art_dex_file_loader.h"
30#include "dex/dex_file_loader.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080031#include "dex2oat_environment_test.h"
32#include "dexopt_test.h"
33#include "gc/space/image_space.h"
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000034#include "hidden_api.h"
Vladimir Markod3d00c02019-11-07 15:09:07 +000035#include "oat.h"
Calin Juravle028c7ef2021-05-26 15:37:00 -070036#include "profile/profile_compilation_info.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080037
38namespace art {
39void DexoptTest::SetUp() {
40 ReserveImageSpace();
41 Dex2oatEnvironmentTest::SetUp();
42}
43
44void DexoptTest::PreRuntimeCreate() {
45 std::string error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -080046 UnreserveImageSpace();
47}
48
49void DexoptTest::PostRuntimeCreate() {
50 ReserveImageSpace();
51}
52
Vladimir Marko00fe35e2018-12-03 18:43:54 +000053bool DexoptTest::Dex2Oat(const std::vector<std::string>& args, std::string* error_msg) {
Vladimir Marko00fe35e2018-12-03 18:43:54 +000054 std::vector<std::string> argv;
Vladimir Marko7a85e702018-12-03 18:47:23 +000055 if (!CommonRuntimeTest::StartDex2OatCommandLine(&argv, error_msg)) {
56 return false;
Vladimir Marko00fe35e2018-12-03 18:43:54 +000057 }
Vladimir Marko00fe35e2018-12-03 18:43:54 +000058
Vladimir Marko7a85e702018-12-03 18:47:23 +000059 Runtime* runtime = Runtime::Current();
David Brazdil6dfdfef2019-04-11 17:39:11 +010060 if (runtime->GetHiddenApiEnforcementPolicy() == hiddenapi::EnforcementPolicy::kEnabled) {
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000061 argv.push_back("--runtime-arg");
David Brazdil6dfdfef2019-04-11 17:39:11 +010062 argv.push_back("-Xhidden-api-policy:enabled");
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000063 }
64
65 if (!kIsTargetBuild) {
66 argv.push_back("--host");
67 }
68
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000069 argv.insert(argv.end(), args.begin(), args.end());
70
71 std::string command_line(android::base::Join(argv, ' '));
72 return Exec(argv, error_msg);
73}
74
David Srbeckycf0c6ef2020-02-05 16:25:36 +000075std::string DexoptTest::GenerateAlternateImage(const std::string& scratch_dir) {
76 std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames();
77 std::vector<std::string> libcore_dex_locations = GetLibCoreDexLocations();
78
79 std::string image_dir = scratch_dir + GetInstructionSetString(kRuntimeISA);
80 int mkdir_result = mkdir(image_dir.c_str(), 0700);
81 CHECK_EQ(0, mkdir_result) << image_dir.c_str();
82
83 std::vector<std::string> extra_args {
84 "--compiler-filter=verify",
85 android::base::StringPrintf("--base=0x%08x", ART_BASE_ADDRESS),
86 };
87 std::string filename_prefix = image_dir + "/boot-interpreter";
88 ArrayRef<const std::string> dex_files(libcore_dex_files);
89 ArrayRef<const std::string> dex_locations(libcore_dex_locations);
90 std::string error_msg;
91 bool ok = CompileBootImage(extra_args, filename_prefix, dex_files, dex_locations, &error_msg);
92 EXPECT_TRUE(ok) << error_msg;
93
94 return scratch_dir + "boot-interpreter.art";
95}
96
Calin Juravle36eb3132017-01-13 16:32:38 -080097void DexoptTest::GenerateOatForTest(const std::string& dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +010098 const std::string& oat_location,
Calin Juravle357c66d2017-05-04 01:57:17 +000099 CompilerFilter::Filter filter,
Calin Juravle5f9a8012018-02-12 20:27:46 -0800100 bool with_alternate_image,
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000101 const char* compilation_reason,
102 const std::vector<std::string>& extra_args) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800103 std::vector<std::string> args;
104 args.push_back("--dex-file=" + dex_location);
105 args.push_back("--oat-file=" + oat_location);
106 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
107 args.push_back("--runtime-arg");
108
109 // Use -Xnorelocate regardless of the relocate argument.
110 // We control relocation by redirecting the dalvik cache when needed
111 // rather than use this flag.
112 args.push_back("-Xnorelocate");
113
Mathieu Chartierd0af56c2017-02-17 12:56:25 -0800114 ScratchFile profile_file;
115 if (CompilerFilter::DependsOnProfile(filter)) {
Calin Juravle028c7ef2021-05-26 15:37:00 -0700116 // Create a profile with some basic content so that dex2oat
117 // doesn't get an empty profile and changes the filter to verify.
118 std::string error_msg;
119 std::vector<std::unique_ptr<const DexFile>> dex_files;
120 const ArtDexFileLoader dex_file_loader;
121 ASSERT_TRUE(dex_file_loader.Open(
122 dex_location.c_str(), dex_location.c_str(), /*verify=*/ false, /*verify_checksum=*/ false,
123 &error_msg, &dex_files));
124 EXPECT_GE(dex_files.size(), 1U);
125 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
126 ProfileCompilationInfo info;
127
128 info.AddClass(*dex_file, dex::TypeIndex(0));
129
130 ASSERT_TRUE(info.Save(profile_file.GetFd()));
131 ASSERT_EQ(0, profile_file.GetFile()->Flush());
132
133 // Set the argument
Mathieu Chartierd0af56c2017-02-17 12:56:25 -0800134 args.push_back("--profile-file=" + profile_file.GetFilename());
135 }
136
Calin Juravle36eb3132017-01-13 16:32:38 -0800137 std::string image_location = GetImageLocation();
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000138 std::optional<ScratchDir> scratch;
Calin Juravle36eb3132017-01-13 16:32:38 -0800139 if (with_alternate_image) {
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000140 scratch.emplace(); // Create the scratch directory for the generated boot image.
141 std::string alternate_image_location = GenerateAlternateImage(scratch->GetPath());
142 args.push_back("--boot-image=" + alternate_image_location);
Calin Juravle36eb3132017-01-13 16:32:38 -0800143 }
144
Calin Juravle5f9a8012018-02-12 20:27:46 -0800145 if (compilation_reason != nullptr) {
146 args.push_back("--compilation-reason=" + std::string(compilation_reason));
147 }
148
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000149 args.insert(args.end(), extra_args.begin(), extra_args.end());
150
Calin Juravle36eb3132017-01-13 16:32:38 -0800151 std::string error_msg;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000152 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800153
Calin Juravle36eb3132017-01-13 16:32:38 -0800154 // Verify the odex file was generated as expected.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100155 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100156 oat_location.c_str(),
Calin Juravle36eb3132017-01-13 16:32:38 -0800157 oat_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100158 /*executable=*/ false,
159 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000160 dex_location,
Calin Juravle36eb3132017-01-13 16:32:38 -0800161 &error_msg));
162 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800163 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
164
Calin Juravle36eb3132017-01-13 16:32:38 -0800165 if (CompilerFilter::DependsOnImageChecksum(filter)) {
Vladimir Marko436c6f52019-07-25 14:50:14 +0100166 const OatHeader& oat_header = odex_file->GetOatHeader();
167 const char* oat_bcp = oat_header.GetStoreValueByKey(OatHeader::kBootClassPathKey);
168 ASSERT_TRUE(oat_bcp != nullptr);
169 ASSERT_EQ(oat_bcp, android::base::Join(Runtime::Current()->GetBootClassPathLocations(), ':'));
Vladimir Marko0ace5632018-12-14 11:11:47 +0000170 const char* checksums = oat_header.GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
171 ASSERT_TRUE(checksums != nullptr);
Vladimir Marko436c6f52019-07-25 14:50:14 +0100172
173 bool match = gc::space::ImageSpace::VerifyBootClassPathChecksums(
174 checksums,
175 oat_bcp,
Victor Hsieh61ffd042021-05-20 15:14:25 -0700176 ArrayRef<const std::string>(&image_location, 1),
Vladimir Marko436c6f52019-07-25 14:50:14 +0100177 ArrayRef<const std::string>(Runtime::Current()->GetBootClassPathLocations()),
178 ArrayRef<const std::string>(Runtime::Current()->GetBootClassPath()),
Victor Hsieha09d8b72021-05-24 14:21:55 -0700179 ArrayRef<const int>(Runtime::Current()->GetBootClassPathFds()),
Vladimir Marko436c6f52019-07-25 14:50:14 +0100180 kRuntimeISA,
Vladimir Marko436c6f52019-07-25 14:50:14 +0100181 &error_msg);
182 ASSERT_EQ(!with_alternate_image, match) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800183 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800184}
185
186void DexoptTest::GenerateOdexForTest(const std::string& dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +0100187 const std::string& odex_location,
188 CompilerFilter::Filter filter,
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000189 const char* compilation_reason,
190 const std::vector<std::string>& extra_args) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800191 GenerateOatForTest(dex_location,
192 odex_location,
193 filter,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100194 /*with_alternate_image=*/ false,
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000195 compilation_reason,
196 extra_args);
Calin Juravle36eb3132017-01-13 16:32:38 -0800197}
198
199void DexoptTest::GenerateOatForTest(const char* dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +0100200 CompilerFilter::Filter filter,
201 bool with_alternate_image) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800202 std::string oat_location;
203 std::string error_msg;
204 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
205 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
206 GenerateOatForTest(dex_location,
207 oat_location,
208 filter,
Calin Juravle36eb3132017-01-13 16:32:38 -0800209 with_alternate_image);
210}
211
212void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100213 GenerateOatForTest(dex_location, filter, /*with_alternate_image=*/ false);
Calin Juravle36eb3132017-01-13 16:32:38 -0800214}
215
Calin Juravle36eb3132017-01-13 16:32:38 -0800216void DexoptTest::ReserveImageSpace() {
217 MemMap::Init();
218
219 // Ensure a chunk of memory is reserved for the image space.
Christopher Ferris77b38df2018-01-18 16:16:49 -0800220 uint64_t reservation_start = ART_BASE_ADDRESS;
221 uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Calin Juravle36eb3132017-01-13 16:32:38 -0800222
Christopher Ferris71884782022-03-26 15:37:43 -0700223 std::vector<android::procinfo::MapInfo> maps;
224 ASSERT_TRUE(android::procinfo::ReadProcessMaps(getpid(), &maps));
225 for (const android::procinfo::MapInfo& map_info : maps) {
226 ReserveImageSpaceChunk(reservation_start, std::min(map_info.start, reservation_end));
227 reservation_start = std::max(reservation_start, map_info.end);
228 if (reservation_start >= reservation_end) {
229 break;
230 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800231 }
232 ReserveImageSpaceChunk(reservation_start, reservation_end);
233}
234
235void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
236 if (start < end) {
237 std::string error_msg;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100238 image_reservation_.push_back(MemMap::MapAnonymous("image reservation",
239 reinterpret_cast<uint8_t*>(start),
240 end - start,
241 PROT_NONE,
Vladimir Marko11306592018-10-26 14:22:59 +0100242 /*low_4gb=*/ false,
243 /*reuse=*/ false,
244 /*reservation=*/ nullptr,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100245 &error_msg));
246 ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800247 LOG(INFO) << "Reserved space for image " <<
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100248 reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-" <<
249 reinterpret_cast<void*>(image_reservation_.back().End());
Calin Juravle36eb3132017-01-13 16:32:38 -0800250 }
251}
252
253void DexoptTest::UnreserveImageSpace() {
254 image_reservation_.clear();
255}
256
257} // namespace art