blob: b11e3688714f1f01b4612a3e59dae802f17ff355 [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
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000049static std::string ImageLocation() {
50 Runtime* runtime = Runtime::Current();
51 const std::vector<gc::space::ImageSpace*>& image_spaces =
52 runtime->GetHeap()->GetBootImageSpaces();
53 if (image_spaces.empty()) {
54 return "";
55 }
56 return image_spaces[0]->GetImageLocation();
57}
58
59bool DexoptTest::Dex2Oat(const std::vector<std::string>& args, std::string* error_msg) {
60 Runtime* runtime = Runtime::Current();
61
62 std::vector<std::string> argv;
63 argv.push_back(runtime->GetCompilerExecutable());
64 if (runtime->IsJavaDebuggable()) {
65 argv.push_back("--debuggable");
66 }
67 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
68
David Brazdilf50ac102018-10-17 18:00:06 +010069 if (runtime->GetHiddenApiEnforcementPolicy() != hiddenapi::EnforcementPolicy::kDisabled) {
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000070 argv.push_back("--runtime-arg");
71 argv.push_back("-Xhidden-api-checks");
72 }
73
74 if (!kIsTargetBuild) {
75 argv.push_back("--host");
76 }
77
78 argv.push_back("--boot-image=" + ImageLocation());
79
80 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
81 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
82
83 argv.insert(argv.end(), args.begin(), args.end());
84
85 std::string command_line(android::base::Join(argv, ' '));
86 return Exec(argv, error_msg);
87}
88
Calin Juravle36eb3132017-01-13 16:32:38 -080089void DexoptTest::GenerateOatForTest(const std::string& dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +010090 const std::string& oat_location,
Calin Juravle357c66d2017-05-04 01:57:17 +000091 CompilerFilter::Filter filter,
Calin Juravle5f9a8012018-02-12 20:27:46 -080092 bool with_alternate_image,
93 const char* compilation_reason) {
Calin Juravle36eb3132017-01-13 16:32:38 -080094 std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA));
95 std::string dalvik_cache_tmp = dalvik_cache + ".redirected";
Calin Juravle36eb3132017-01-13 16:32:38 -080096
97 std::vector<std::string> args;
98 args.push_back("--dex-file=" + dex_location);
99 args.push_back("--oat-file=" + oat_location);
100 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
101 args.push_back("--runtime-arg");
102
103 // Use -Xnorelocate regardless of the relocate argument.
104 // We control relocation by redirecting the dalvik cache when needed
105 // rather than use this flag.
106 args.push_back("-Xnorelocate");
107
Mathieu Chartierd0af56c2017-02-17 12:56:25 -0800108 ScratchFile profile_file;
109 if (CompilerFilter::DependsOnProfile(filter)) {
110 args.push_back("--profile-file=" + profile_file.GetFilename());
111 }
112
Calin Juravle36eb3132017-01-13 16:32:38 -0800113 std::string image_location = GetImageLocation();
114 if (with_alternate_image) {
115 args.push_back("--boot-image=" + GetImageLocation2());
116 }
117
Calin Juravle5f9a8012018-02-12 20:27:46 -0800118 if (compilation_reason != nullptr) {
119 args.push_back("--compilation-reason=" + std::string(compilation_reason));
120 }
121
Calin Juravle36eb3132017-01-13 16:32:38 -0800122 std::string error_msg;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000123 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800124
Calin Juravle36eb3132017-01-13 16:32:38 -0800125 // Verify the odex file was generated as expected.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100126 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100127 oat_location.c_str(),
Calin Juravle36eb3132017-01-13 16:32:38 -0800128 oat_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100129 /*executable=*/ false,
130 /*low_4gb=*/ false,
Calin Juravle36eb3132017-01-13 16:32:38 -0800131 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100132 /*reservation=*/ nullptr,
Calin Juravle36eb3132017-01-13 16:32:38 -0800133 &error_msg));
134 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800135 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
136
137 std::unique_ptr<ImageHeader> image_header(
138 gc::space::ImageSpace::ReadImageHeader(image_location.c_str(),
139 kRuntimeISA,
140 &error_msg));
141 ASSERT_TRUE(image_header != nullptr) << error_msg;
142 const OatHeader& oat_header = odex_file->GetOatHeader();
Richard Uhlerbc26b722017-03-10 14:27:10 +0000143 uint32_t combined_checksum = image_header->GetOatChecksum();
Calin Juravle36eb3132017-01-13 16:32:38 -0800144
145 if (CompilerFilter::DependsOnImageChecksum(filter)) {
146 if (with_alternate_image) {
147 EXPECT_NE(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
148 } else {
149 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
150 }
151 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800152}
153
154void DexoptTest::GenerateOdexForTest(const std::string& dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +0100155 const std::string& odex_location,
156 CompilerFilter::Filter filter,
157 const char* compilation_reason) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800158 GenerateOatForTest(dex_location,
159 odex_location,
160 filter,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100161 /*with_alternate_image=*/ false,
Calin Juravle5f9a8012018-02-12 20:27:46 -0800162 compilation_reason);
Calin Juravle36eb3132017-01-13 16:32:38 -0800163}
164
165void DexoptTest::GenerateOatForTest(const char* dex_location,
Vladimir Markoe0669322018-09-03 15:44:54 +0100166 CompilerFilter::Filter filter,
167 bool with_alternate_image) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800168 std::string oat_location;
169 std::string error_msg;
170 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
171 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
172 GenerateOatForTest(dex_location,
173 oat_location,
174 filter,
Calin Juravle36eb3132017-01-13 16:32:38 -0800175 with_alternate_image);
176}
177
178void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100179 GenerateOatForTest(dex_location, filter, /*with_alternate_image=*/ false);
Calin Juravle36eb3132017-01-13 16:32:38 -0800180}
181
Calin Juravle36eb3132017-01-13 16:32:38 -0800182void DexoptTest::ReserveImageSpace() {
183 MemMap::Init();
184
185 // Ensure a chunk of memory is reserved for the image space.
186 // The reservation_end includes room for the main space that has to come
187 // right after the image in case of the GSS collector.
Christopher Ferris77b38df2018-01-18 16:16:49 -0800188 uint64_t reservation_start = ART_BASE_ADDRESS;
189 uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Calin Juravle36eb3132017-01-13 16:32:38 -0800190
191 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
192 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800193 for (BacktraceMap::iterator it = map->begin();
Calin Juravle36eb3132017-01-13 16:32:38 -0800194 reservation_start < reservation_end && it != map->end(); ++it) {
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800195 const backtrace_map_t* entry = *it;
196 ReserveImageSpaceChunk(reservation_start, std::min(entry->start, reservation_end));
197 reservation_start = std::max(reservation_start, entry->end);
Calin Juravle36eb3132017-01-13 16:32:38 -0800198 }
199 ReserveImageSpaceChunk(reservation_start, reservation_end);
200}
201
202void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
203 if (start < end) {
204 std::string error_msg;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100205 image_reservation_.push_back(MemMap::MapAnonymous("image reservation",
206 reinterpret_cast<uint8_t*>(start),
207 end - start,
208 PROT_NONE,
Vladimir Marko11306592018-10-26 14:22:59 +0100209 /*low_4gb=*/ false,
210 /*reuse=*/ false,
211 /*reservation=*/ nullptr,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100212 &error_msg));
213 ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800214 LOG(INFO) << "Reserved space for image " <<
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100215 reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-" <<
216 reinterpret_cast<void*>(image_reservation_.back().End());
Calin Juravle36eb3132017-01-13 16:32:38 -0800217 }
218}
219
220void DexoptTest::UnreserveImageSpace() {
221 image_reservation_.clear();
222}
223
224} // namespace art