blob: 9b5b473da09a22e708e3f9897b7ad4f5bafc948e [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
20#include <backtrace/BacktraceMap.h>
21#include <gtest/gtest.h>
22
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"
29#include "dex2oat_environment_test.h"
30#include "dexopt_test.h"
31#include "gc/space/image_space.h"
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000032#include "hidden_api.h"
Vladimir Markod3d00c02019-11-07 15:09:07 +000033#include "oat.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080034
35namespace art {
36void DexoptTest::SetUp() {
37 ReserveImageSpace();
38 Dex2oatEnvironmentTest::SetUp();
39}
40
41void DexoptTest::PreRuntimeCreate() {
42 std::string error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -080043 UnreserveImageSpace();
44}
45
46void DexoptTest::PostRuntimeCreate() {
47 ReserveImageSpace();
48}
49
Vladimir Marko00fe35e2018-12-03 18:43:54 +000050bool DexoptTest::Dex2Oat(const std::vector<std::string>& args, std::string* error_msg) {
Vladimir Marko00fe35e2018-12-03 18:43:54 +000051 std::vector<std::string> argv;
Vladimir Marko7a85e702018-12-03 18:47:23 +000052 if (!CommonRuntimeTest::StartDex2OatCommandLine(&argv, error_msg)) {
53 return false;
Vladimir Marko00fe35e2018-12-03 18:43:54 +000054 }
Vladimir Marko00fe35e2018-12-03 18:43:54 +000055
Vladimir Marko7a85e702018-12-03 18:47:23 +000056 Runtime* runtime = Runtime::Current();
David Brazdil6dfdfef2019-04-11 17:39:11 +010057 if (runtime->GetHiddenApiEnforcementPolicy() == hiddenapi::EnforcementPolicy::kEnabled) {
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000058 argv.push_back("--runtime-arg");
David Brazdil6dfdfef2019-04-11 17:39:11 +010059 argv.push_back("-Xhidden-api-policy:enabled");
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000060 }
61
62 if (!kIsTargetBuild) {
63 argv.push_back("--host");
64 }
65
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000066 argv.insert(argv.end(), args.begin(), args.end());
67
68 std::string command_line(android::base::Join(argv, ' '));
69 return Exec(argv, error_msg);
70}
71
David Srbeckycf0c6ef2020-02-05 16:25:36 +000072std::string DexoptTest::GenerateAlternateImage(const std::string& scratch_dir) {
73 std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames();
74 std::vector<std::string> libcore_dex_locations = GetLibCoreDexLocations();
75
76 std::string image_dir = scratch_dir + GetInstructionSetString(kRuntimeISA);
77 int mkdir_result = mkdir(image_dir.c_str(), 0700);
78 CHECK_EQ(0, mkdir_result) << image_dir.c_str();
79
80 std::vector<std::string> extra_args {
81 "--compiler-filter=verify",
82 android::base::StringPrintf("--base=0x%08x", ART_BASE_ADDRESS),
83 };
84 std::string filename_prefix = image_dir + "/boot-interpreter";
85 ArrayRef<const std::string> dex_files(libcore_dex_files);
86 ArrayRef<const std::string> dex_locations(libcore_dex_locations);
87 std::string error_msg;
88 bool ok = CompileBootImage(extra_args, filename_prefix, dex_files, dex_locations, &error_msg);
89 EXPECT_TRUE(ok) << error_msg;
90
91 return scratch_dir + "boot-interpreter.art";
92}
93
Calin Juravle36eb3132017-01-13 16:32:38 -080094void DexoptTest::GenerateOatForTest(const std::string& dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +010095 const std::string& oat_location,
Calin Juravle357c66d2017-05-04 01:57:17 +000096 CompilerFilter::Filter filter,
Calin Juravle5f9a8012018-02-12 20:27:46 -080097 bool with_alternate_image,
Nicolas Geoffray35de14b2019-01-10 13:10:36 +000098 const char* compilation_reason,
99 const std::vector<std::string>& extra_args) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800100 std::vector<std::string> args;
101 args.push_back("--dex-file=" + dex_location);
102 args.push_back("--oat-file=" + oat_location);
103 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
104 args.push_back("--runtime-arg");
105
106 // Use -Xnorelocate regardless of the relocate argument.
107 // We control relocation by redirecting the dalvik cache when needed
108 // rather than use this flag.
109 args.push_back("-Xnorelocate");
110
Mathieu Chartierd0af56c2017-02-17 12:56:25 -0800111 ScratchFile profile_file;
112 if (CompilerFilter::DependsOnProfile(filter)) {
113 args.push_back("--profile-file=" + profile_file.GetFilename());
114 }
115
Calin Juravle36eb3132017-01-13 16:32:38 -0800116 std::string image_location = GetImageLocation();
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000117 std::optional<ScratchDir> scratch;
Calin Juravle36eb3132017-01-13 16:32:38 -0800118 if (with_alternate_image) {
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000119 scratch.emplace(); // Create the scratch directory for the generated boot image.
120 std::string alternate_image_location = GenerateAlternateImage(scratch->GetPath());
121 args.push_back("--boot-image=" + alternate_image_location);
Calin Juravle36eb3132017-01-13 16:32:38 -0800122 }
123
Calin Juravle5f9a8012018-02-12 20:27:46 -0800124 if (compilation_reason != nullptr) {
125 args.push_back("--compilation-reason=" + std::string(compilation_reason));
126 }
127
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000128 args.insert(args.end(), extra_args.begin(), extra_args.end());
129
Calin Juravle36eb3132017-01-13 16:32:38 -0800130 std::string error_msg;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000131 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800132
Calin Juravle36eb3132017-01-13 16:32:38 -0800133 // Verify the odex file was generated as expected.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100134 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100135 oat_location.c_str(),
Calin Juravle36eb3132017-01-13 16:32:38 -0800136 oat_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100137 /*executable=*/ false,
138 /*low_4gb=*/ false,
Vladimir Markob7bf8432019-12-03 13:18:50 +0000139 dex_location,
Calin Juravle36eb3132017-01-13 16:32:38 -0800140 &error_msg));
141 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800142 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
143
Calin Juravle36eb3132017-01-13 16:32:38 -0800144 if (CompilerFilter::DependsOnImageChecksum(filter)) {
Vladimir Marko436c6f52019-07-25 14:50:14 +0100145 const OatHeader& oat_header = odex_file->GetOatHeader();
146 const char* oat_bcp = oat_header.GetStoreValueByKey(OatHeader::kBootClassPathKey);
147 ASSERT_TRUE(oat_bcp != nullptr);
148 ASSERT_EQ(oat_bcp, android::base::Join(Runtime::Current()->GetBootClassPathLocations(), ':'));
Vladimir Marko0ace5632018-12-14 11:11:47 +0000149 const char* checksums = oat_header.GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
150 ASSERT_TRUE(checksums != nullptr);
Vladimir Marko436c6f52019-07-25 14:50:14 +0100151
152 bool match = gc::space::ImageSpace::VerifyBootClassPathChecksums(
153 checksums,
154 oat_bcp,
Victor Hsieh61ffd042021-05-20 15:14:25 -0700155 ArrayRef<const std::string>(&image_location, 1),
Vladimir Marko436c6f52019-07-25 14:50:14 +0100156 ArrayRef<const std::string>(Runtime::Current()->GetBootClassPathLocations()),
157 ArrayRef<const std::string>(Runtime::Current()->GetBootClassPath()),
158 kRuntimeISA,
Vladimir Marko436c6f52019-07-25 14:50:14 +0100159 &error_msg);
160 ASSERT_EQ(!with_alternate_image, match) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800161 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800162}
163
164void DexoptTest::GenerateOdexForTest(const std::string& dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +0100165 const std::string& odex_location,
166 CompilerFilter::Filter filter,
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000167 const char* compilation_reason,
168 const std::vector<std::string>& extra_args) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800169 GenerateOatForTest(dex_location,
170 odex_location,
171 filter,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100172 /*with_alternate_image=*/ false,
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000173 compilation_reason,
174 extra_args);
Calin Juravle36eb3132017-01-13 16:32:38 -0800175}
176
177void DexoptTest::GenerateOatForTest(const char* dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +0100178 CompilerFilter::Filter filter,
179 bool with_alternate_image) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800180 std::string oat_location;
181 std::string error_msg;
182 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
183 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
184 GenerateOatForTest(dex_location,
185 oat_location,
186 filter,
Calin Juravle36eb3132017-01-13 16:32:38 -0800187 with_alternate_image);
188}
189
190void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100191 GenerateOatForTest(dex_location, filter, /*with_alternate_image=*/ false);
Calin Juravle36eb3132017-01-13 16:32:38 -0800192}
193
Calin Juravle36eb3132017-01-13 16:32:38 -0800194void DexoptTest::ReserveImageSpace() {
195 MemMap::Init();
196
197 // Ensure a chunk of memory is reserved for the image space.
Christopher Ferris77b38df2018-01-18 16:16:49 -0800198 uint64_t reservation_start = ART_BASE_ADDRESS;
199 uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Calin Juravle36eb3132017-01-13 16:32:38 -0800200
201 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
202 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800203 for (BacktraceMap::iterator it = map->begin();
Calin Juravle36eb3132017-01-13 16:32:38 -0800204 reservation_start < reservation_end && it != map->end(); ++it) {
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800205 const backtrace_map_t* entry = *it;
206 ReserveImageSpaceChunk(reservation_start, std::min(entry->start, reservation_end));
207 reservation_start = std::max(reservation_start, entry->end);
Calin Juravle36eb3132017-01-13 16:32:38 -0800208 }
209 ReserveImageSpaceChunk(reservation_start, reservation_end);
210}
211
212void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
213 if (start < end) {
214 std::string error_msg;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100215 image_reservation_.push_back(MemMap::MapAnonymous("image reservation",
216 reinterpret_cast<uint8_t*>(start),
217 end - start,
218 PROT_NONE,
Vladimir Marko11306592018-10-26 14:22:59 +0100219 /*low_4gb=*/ false,
220 /*reuse=*/ false,
221 /*reservation=*/ nullptr,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100222 &error_msg));
223 ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800224 LOG(INFO) << "Reserved space for image " <<
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100225 reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-" <<
226 reinterpret_cast<void*>(image_reservation_.back().End());
Calin Juravle36eb3132017-01-13 16:32:38 -0800227 }
228}
229
230void DexoptTest::UnreserveImageSpace() {
231 image_reservation_.clear();
232}
233
234} // namespace art