blob: 9c0ac8fc541e4e8d47dd8f6298aa172368939fe5 [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"
Calin Juravle36eb3132017-01-13 16:32:38 -080033
34namespace art {
35void DexoptTest::SetUp() {
36 ReserveImageSpace();
37 Dex2oatEnvironmentTest::SetUp();
38}
39
40void DexoptTest::PreRuntimeCreate() {
41 std::string error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -080042 UnreserveImageSpace();
43}
44
45void DexoptTest::PostRuntimeCreate() {
46 ReserveImageSpace();
47}
48
Vladimir Marko00fe35e2018-12-03 18:43:54 +000049bool DexoptTest::Dex2Oat(const std::vector<std::string>& args, std::string* error_msg) {
Vladimir Marko00fe35e2018-12-03 18:43:54 +000050 std::vector<std::string> argv;
Vladimir Marko7a85e702018-12-03 18:47:23 +000051 if (!CommonRuntimeTest::StartDex2OatCommandLine(&argv, error_msg)) {
52 return false;
Vladimir Marko00fe35e2018-12-03 18:43:54 +000053 }
Vladimir Marko00fe35e2018-12-03 18:43:54 +000054
Vladimir Marko7a85e702018-12-03 18:47:23 +000055 Runtime* runtime = Runtime::Current();
David Brazdilf50ac102018-10-17 18:00:06 +010056 if (runtime->GetHiddenApiEnforcementPolicy() != hiddenapi::EnforcementPolicy::kDisabled) {
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000057 argv.push_back("--runtime-arg");
58 argv.push_back("-Xhidden-api-checks");
59 }
60
61 if (!kIsTargetBuild) {
62 argv.push_back("--host");
63 }
64
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000065 argv.insert(argv.end(), args.begin(), args.end());
66
67 std::string command_line(android::base::Join(argv, ' '));
68 return Exec(argv, error_msg);
69}
70
Calin Juravle36eb3132017-01-13 16:32:38 -080071void DexoptTest::GenerateOatForTest(const std::string& dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +010072 const std::string& oat_location,
Calin Juravle357c66d2017-05-04 01:57:17 +000073 CompilerFilter::Filter filter,
Calin Juravle5f9a8012018-02-12 20:27:46 -080074 bool with_alternate_image,
Nicolas Geoffray35de14b2019-01-10 13:10:36 +000075 const char* compilation_reason,
76 const std::vector<std::string>& extra_args) {
Calin Juravle36eb3132017-01-13 16:32:38 -080077 std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA));
78 std::string dalvik_cache_tmp = dalvik_cache + ".redirected";
Calin Juravle36eb3132017-01-13 16:32:38 -080079
80 std::vector<std::string> args;
81 args.push_back("--dex-file=" + dex_location);
82 args.push_back("--oat-file=" + oat_location);
83 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
84 args.push_back("--runtime-arg");
85
86 // Use -Xnorelocate regardless of the relocate argument.
87 // We control relocation by redirecting the dalvik cache when needed
88 // rather than use this flag.
89 args.push_back("-Xnorelocate");
90
Mathieu Chartierd0af56c2017-02-17 12:56:25 -080091 ScratchFile profile_file;
92 if (CompilerFilter::DependsOnProfile(filter)) {
93 args.push_back("--profile-file=" + profile_file.GetFilename());
94 }
95
Calin Juravle36eb3132017-01-13 16:32:38 -080096 std::string image_location = GetImageLocation();
97 if (with_alternate_image) {
98 args.push_back("--boot-image=" + GetImageLocation2());
99 }
100
Calin Juravle5f9a8012018-02-12 20:27:46 -0800101 if (compilation_reason != nullptr) {
102 args.push_back("--compilation-reason=" + std::string(compilation_reason));
103 }
104
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000105 args.insert(args.end(), extra_args.begin(), extra_args.end());
106
Calin Juravle36eb3132017-01-13 16:32:38 -0800107 std::string error_msg;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000108 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800109
Calin Juravle36eb3132017-01-13 16:32:38 -0800110 // Verify the odex file was generated as expected.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100111 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100112 oat_location.c_str(),
Calin Juravle36eb3132017-01-13 16:32:38 -0800113 oat_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100114 /*executable=*/ false,
115 /*low_4gb=*/ false,
Calin Juravle36eb3132017-01-13 16:32:38 -0800116 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100117 /*reservation=*/ nullptr,
Calin Juravle36eb3132017-01-13 16:32:38 -0800118 &error_msg));
119 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800120 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
121
Vladimir Marko0ace5632018-12-14 11:11:47 +0000122 std::string boot_image_checksums = gc::space::ImageSpace::GetBootClassPathChecksums(
123 Runtime::Current()->GetBootClassPath(), image_location, kRuntimeISA, &error_msg);
124 ASSERT_FALSE(boot_image_checksums.empty()) << error_msg;
125
Calin Juravle36eb3132017-01-13 16:32:38 -0800126 const OatHeader& oat_header = odex_file->GetOatHeader();
Calin Juravle36eb3132017-01-13 16:32:38 -0800127
128 if (CompilerFilter::DependsOnImageChecksum(filter)) {
Vladimir Marko0ace5632018-12-14 11:11:47 +0000129 const char* checksums = oat_header.GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
130 ASSERT_TRUE(checksums != nullptr);
Calin Juravle36eb3132017-01-13 16:32:38 -0800131 if (with_alternate_image) {
Vladimir Marko0ace5632018-12-14 11:11:47 +0000132 EXPECT_NE(boot_image_checksums, checksums);
Calin Juravle36eb3132017-01-13 16:32:38 -0800133 } else {
Vladimir Marko0ace5632018-12-14 11:11:47 +0000134 EXPECT_EQ(boot_image_checksums, checksums);
Calin Juravle36eb3132017-01-13 16:32:38 -0800135 }
136 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800137}
138
139void DexoptTest::GenerateOdexForTest(const std::string& dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +0100140 const std::string& odex_location,
141 CompilerFilter::Filter filter,
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000142 const char* compilation_reason,
143 const std::vector<std::string>& extra_args) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800144 GenerateOatForTest(dex_location,
145 odex_location,
146 filter,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100147 /*with_alternate_image=*/ false,
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000148 compilation_reason,
149 extra_args);
Calin Juravle36eb3132017-01-13 16:32:38 -0800150}
151
152void DexoptTest::GenerateOatForTest(const char* dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +0100153 CompilerFilter::Filter filter,
154 bool with_alternate_image) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800155 std::string oat_location;
156 std::string error_msg;
157 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
158 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
159 GenerateOatForTest(dex_location,
160 oat_location,
161 filter,
Calin Juravle36eb3132017-01-13 16:32:38 -0800162 with_alternate_image);
163}
164
165void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100166 GenerateOatForTest(dex_location, filter, /*with_alternate_image=*/ false);
Calin Juravle36eb3132017-01-13 16:32:38 -0800167}
168
Calin Juravle36eb3132017-01-13 16:32:38 -0800169void DexoptTest::ReserveImageSpace() {
170 MemMap::Init();
171
172 // Ensure a chunk of memory is reserved for the image space.
173 // The reservation_end includes room for the main space that has to come
174 // right after the image in case of the GSS collector.
Christopher Ferris77b38df2018-01-18 16:16:49 -0800175 uint64_t reservation_start = ART_BASE_ADDRESS;
176 uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Calin Juravle36eb3132017-01-13 16:32:38 -0800177
178 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
179 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800180 for (BacktraceMap::iterator it = map->begin();
Calin Juravle36eb3132017-01-13 16:32:38 -0800181 reservation_start < reservation_end && it != map->end(); ++it) {
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800182 const backtrace_map_t* entry = *it;
183 ReserveImageSpaceChunk(reservation_start, std::min(entry->start, reservation_end));
184 reservation_start = std::max(reservation_start, entry->end);
Calin Juravle36eb3132017-01-13 16:32:38 -0800185 }
186 ReserveImageSpaceChunk(reservation_start, reservation_end);
187}
188
189void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
190 if (start < end) {
191 std::string error_msg;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100192 image_reservation_.push_back(MemMap::MapAnonymous("image reservation",
193 reinterpret_cast<uint8_t*>(start),
194 end - start,
195 PROT_NONE,
Vladimir Marko11306592018-10-26 14:22:59 +0100196 /*low_4gb=*/ false,
197 /*reuse=*/ false,
198 /*reservation=*/ nullptr,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100199 &error_msg));
200 ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800201 LOG(INFO) << "Reserved space for image " <<
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100202 reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-" <<
203 reinterpret_cast<void*>(image_reservation_.back().End());
Calin Juravle36eb3132017-01-13 16:32:38 -0800204 }
205}
206
207void DexoptTest::UnreserveImageSpace() {
208 image_reservation_.clear();
209}
210
211} // namespace art