blob: a1ee68faebde2f23f7d83db38b815814967660ef [file] [log] [blame]
Ian Rogerse63db272014-07-15 15:36:11 -07001/*
2 * Copyright (C) 2011 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 "common_compiler_test.h"
18
Ian Rogersd582fa42014-11-05 23:46:43 -080019#include "arch/instruction_set_features.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_field-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080021#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "class_linker.h"
24#include "compiled_method.h"
25#include "dex/quick_compiler_callbacks.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080026#include "dex/verification_results.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000028#include "driver/compiler_options.h"
Ian Rogerse63db272014-07-15 15:36:11 -070029#include "interpreter/interpreter.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "mirror/class_loader.h"
31#include "mirror/class-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070032#include "mirror/dex_cache.h"
33#include "mirror/object-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010034#include "oat_quick_method_header.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070035#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070036#include "thread-current-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "utils.h"
38
39namespace art {
40
Ian Rogerse63db272014-07-15 15:36:11 -070041CommonCompilerTest::CommonCompilerTest() {}
42CommonCompilerTest::~CommonCompilerTest() {}
43
Mathieu Chartiere401d142015-04-22 13:56:20 -070044void CommonCompilerTest::MakeExecutable(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -070045 CHECK(method != nullptr);
46
47 const CompiledMethod* compiled_method = nullptr;
48 if (!method->IsAbstract()) {
49 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
50 const DexFile& dex_file = *dex_cache->GetDexFile();
51 compiled_method =
52 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
53 method->GetDexMethodIndex()));
54 }
Calin Juravlee0ac1152017-02-13 19:03:47 -080055 // If the code size is 0 it means the method was skipped due to profile guided compilation.
56 if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0u) {
Vladimir Marko35831e82015-09-11 11:59:18 +010057 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070058 const uint32_t code_size = code.size();
Vladimir Marko35831e82015-09-11 11:59:18 +010059 ArrayRef<const uint8_t> vmap_table = compiled_method->GetVmapTable();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070060 const uint32_t vmap_table_offset = vmap_table.empty() ? 0u
Vladimir Marko35831e82015-09-11 11:59:18 +010061 : sizeof(OatQuickMethodHeader) + vmap_table.size();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070062 // The method info is directly before the vmap table.
63 ArrayRef<const uint8_t> method_info = compiled_method->GetMethodInfo();
64 const uint32_t method_info_offset = method_info.empty() ? 0u
65 : vmap_table_offset + method_info.size();
66
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010067 OatQuickMethodHeader method_header(vmap_table_offset,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070068 method_info_offset,
Elliott Hughes956af0f2014-12-11 14:34:28 -080069 compiled_method->GetFrameSizeInBytes(),
70 compiled_method->GetCoreSpillMask(),
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010071 compiled_method->GetFpSpillMask(),
72 code_size);
Ian Rogerse63db272014-07-15 15:36:11 -070073
Elliott Hughes956af0f2014-12-11 14:34:28 -080074 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
75 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
Vladimir Marko562ff442015-10-27 18:51:20 +000076 const size_t max_padding = GetInstructionSetAlignment(compiled_method->GetInstructionSet());
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070077 const size_t size = method_info.size() + vmap_table.size() + sizeof(method_header) + code_size;
Vladimir Marko562ff442015-10-27 18:51:20 +000078 chunk->reserve(size + max_padding);
Elliott Hughes956af0f2014-12-11 14:34:28 -080079 chunk->resize(sizeof(method_header));
80 memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
Vladimir Marko35831e82015-09-11 11:59:18 +010081 chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end());
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070082 chunk->insert(chunk->begin(), method_info.begin(), method_info.end());
Vladimir Marko35831e82015-09-11 11:59:18 +010083 chunk->insert(chunk->end(), code.begin(), code.end());
Vladimir Marko562ff442015-10-27 18:51:20 +000084 CHECK_EQ(chunk->size(), size);
85 const void* unaligned_code_ptr = chunk->data() + (size - code_size);
86 size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr));
87 size_t padding = compiled_method->AlignCode(offset) - offset;
88 // Make sure no resizing takes place.
89 CHECK_GE(chunk->capacity(), chunk->size() + padding);
90 chunk->insert(chunk->begin(), padding, 0);
91 const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding;
92 CHECK_EQ(code_ptr, static_cast<const void*>(chunk->data() + (chunk->size() - code_size)));
Vladimir Marko35831e82015-09-11 11:59:18 +010093 MakeExecutable(code_ptr, code.size());
Ian Rogerse63db272014-07-15 15:36:11 -070094 const void* method_code = CompiledMethod::CodePointer(code_ptr,
95 compiled_method->GetInstructionSet());
David Sehr709b0702016-10-13 09:12:37 -070096 LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code;
Elliott Hughes956af0f2014-12-11 14:34:28 -080097 class_linker_->SetEntryPointsToCompiledCode(method, method_code);
Ian Rogerse63db272014-07-15 15:36:11 -070098 } else {
99 // No code? You must mean to go into the interpreter.
100 // Or the generic JNI...
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700101 class_linker_->SetEntryPointsToInterpreter(method);
Ian Rogerse63db272014-07-15 15:36:11 -0700102 }
103}
104
105void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
106 CHECK(code_start != nullptr);
107 CHECK_NE(code_length, 0U);
108 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
109 uintptr_t base = RoundDown(data, kPageSize);
110 uintptr_t limit = RoundUp(data + code_length, kPageSize);
111 uintptr_t len = limit - base;
112 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
113 CHECK_EQ(result, 0);
114
Roland Levillain32430262016-02-01 15:23:20 +0000115 FlushInstructionCache(reinterpret_cast<char*>(base), reinterpret_cast<char*>(base + len));
Ian Rogerse63db272014-07-15 15:36:11 -0700116}
117
Mathieu Chartier0795f232016-09-27 18:43:30 -0700118void CommonCompilerTest::MakeExecutable(ObjPtr<mirror::ClassLoader> class_loader,
119 const char* class_name) {
Ian Rogerse63db272014-07-15 15:36:11 -0700120 std::string class_descriptor(DotToDescriptor(class_name));
121 Thread* self = Thread::Current();
122 StackHandleScope<1> hs(self);
123 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
124 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
125 CHECK(klass != nullptr) << "Class not found " << class_name;
Andreas Gampe542451c2016-07-26 09:02:02 -0700126 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800127 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700128 MakeExecutable(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700129 }
130}
131
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700132// Get the set of image classes given to the compiler-driver in SetUp. Note: the compiler
133// driver assumes ownership of the set, so the test should properly release the set.
134std::unordered_set<std::string>* CommonCompilerTest::GetImageClasses() {
135 // Empty set: by default no classes are retained in the image.
136 return new std::unordered_set<std::string>();
137}
138
139// Get the set of compiled classes given to the compiler-driver in SetUp. Note: the compiler
140// driver assumes ownership of the set, so the test should properly release the set.
141std::unordered_set<std::string>* CommonCompilerTest::GetCompiledClasses() {
142 // Null, no selection of compiled-classes.
143 return nullptr;
144}
145
146// Get the set of compiled methods given to the compiler-driver in SetUp. Note: the compiler
147// driver assumes ownership of the set, so the test should properly release the set.
148std::unordered_set<std::string>* CommonCompilerTest::GetCompiledMethods() {
149 // Null, no selection of compiled-methods.
150 return nullptr;
151}
152
Calin Juravle877fd962016-01-05 14:29:29 +0000153// Get ProfileCompilationInfo that should be passed to the driver.
154ProfileCompilationInfo* CommonCompilerTest::GetProfileCompilationInfo() {
155 // Null, profile information will not be taken into account.
156 return nullptr;
157}
158
Ian Rogerse63db272014-07-15 15:36:11 -0700159void CommonCompilerTest::SetUp() {
160 CommonRuntimeTest::SetUp();
161 {
162 ScopedObjectAccess soa(Thread::Current());
163
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700164 const InstructionSet instruction_set = kRuntimeISA;
Ian Rogerse63db272014-07-15 15:36:11 -0700165 // Take the default set of instruction features from the build.
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800166 instruction_set_features_ = InstructionSetFeatures::FromCppDefines();
Ian Rogerse63db272014-07-15 15:36:11 -0700167
168 runtime_->SetInstructionSet(instruction_set);
169 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
170 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
171 if (!runtime_->HasCalleeSaveMethod(type)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700172 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
Ian Rogerse63db272014-07-15 15:36:11 -0700173 }
174 }
175
Ian Rogerse63db272014-07-15 15:36:11 -0700176 timer_.reset(new CumulativeLogger("Compilation times"));
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800177 CreateCompilerDriver(compiler_kind_, instruction_set);
Ian Rogerse63db272014-07-15 15:36:11 -0700178 }
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800179}
180
Andreas Gampe3f41a012016-02-18 16:53:41 -0800181void CommonCompilerTest::CreateCompilerDriver(Compiler::Kind kind,
182 InstructionSet isa,
183 size_t number_of_threads) {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000184 compiler_options_->boot_image_ = true;
Mathieu Chartierd0af56c2017-02-17 12:56:25 -0800185 compiler_options_->SetCompilerFilter(GetCompilerFilter());
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800186 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
187 verification_results_.get(),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800188 kind,
189 isa,
190 instruction_set_features_.get(),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800191 GetImageClasses(),
192 GetCompiledClasses(),
193 GetCompiledMethods(),
Andreas Gampe3f41a012016-02-18 16:53:41 -0800194 number_of_threads,
Vladimir Marko944da602016-02-19 12:27:55 +0000195 /* dump_stats */ true,
196 /* dump_passes */ true,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800197 timer_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000198 /* swap_fd */ -1,
Calin Juravle877fd962016-01-05 14:29:29 +0000199 GetProfileCompilationInfo()));
Ian Rogerse63db272014-07-15 15:36:11 -0700200 // We typically don't generate an image in unit tests, disable this optimization by default.
201 compiler_driver_->SetSupportBootImageFixup(false);
202}
203
204void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
205 CommonRuntimeTest::SetUpRuntimeOptions(options);
206
207 compiler_options_.reset(new CompilerOptions);
208 verification_results_.reset(new VerificationResults(compiler_options_.get()));
Ian Rogerse63db272014-07-15 15:36:11 -0700209 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700210 CompilerCallbacks::CallbackMode::kCompileApp));
Ian Rogerse63db272014-07-15 15:36:11 -0700211}
212
Roland Levillainbbcc01a2015-06-30 14:16:48 +0100213Compiler::Kind CommonCompilerTest::GetCompilerKind() const {
214 return compiler_kind_;
215}
216
217void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) {
218 compiler_kind_ = compiler_kind;
219}
220
Roland Levillain0d5a2812015-11-13 10:07:31 +0000221InstructionSet CommonCompilerTest::GetInstructionSet() const {
222 DCHECK(compiler_driver_.get() != nullptr);
223 return compiler_driver_->GetInstructionSet();
224}
225
Ian Rogerse63db272014-07-15 15:36:11 -0700226void CommonCompilerTest::TearDown() {
227 timer_.reset();
228 compiler_driver_.reset();
229 callbacks_.reset();
Ian Rogerse63db272014-07-15 15:36:11 -0700230 verification_results_.reset();
231 compiler_options_.reset();
Mathieu Chartier496577f2016-09-20 15:33:31 -0700232 image_reservation_.reset();
Ian Rogerse63db272014-07-15 15:36:11 -0700233
234 CommonRuntimeTest::TearDown();
235}
236
237void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
238 std::string class_descriptor(DotToDescriptor(class_name));
239 Thread* self = Thread::Current();
240 StackHandleScope<1> hs(self);
241 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
242 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
243 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700244 auto pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800245 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700246 CompileMethod(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700247 }
248}
249
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250void CommonCompilerTest::CompileMethod(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -0700251 CHECK(method != nullptr);
252 TimingLogger timings("CommonTest::CompileMethod", false, false);
253 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800254 compiler_driver_->CompileOne(Thread::Current(), method, &timings);
Ian Rogerse63db272014-07-15 15:36:11 -0700255 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
256 MakeExecutable(method);
257}
258
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700259void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
Ian Rogerse63db272014-07-15 15:36:11 -0700260 const char* class_name, const char* method_name,
261 const char* signature) {
262 std::string class_descriptor(DotToDescriptor(class_name));
263 Thread* self = Thread::Current();
264 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
265 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700266 auto pointer_size = class_linker_->GetImagePointerSize();
267 ArtMethod* method = klass->FindDirectMethod(method_name, signature, pointer_size);
Ian Rogerse63db272014-07-15 15:36:11 -0700268 CHECK(method != nullptr) << "Direct method not found: "
269 << class_name << "." << method_name << signature;
270 CompileMethod(method);
271}
272
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700273void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700274 const char* class_name, const char* method_name,
275 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700276 std::string class_descriptor(DotToDescriptor(class_name));
277 Thread* self = Thread::Current();
278 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
279 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700280 auto pointer_size = class_linker_->GetImagePointerSize();
281 ArtMethod* method = klass->FindVirtualMethod(method_name, signature, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700282 CHECK(method != nullptr) << "Virtual method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700283 << class_name << "." << method_name << signature;
284 CompileMethod(method);
285}
286
287void CommonCompilerTest::ReserveImageSpace() {
288 // Reserve where the image will be loaded up front so that other parts of test set up don't
289 // accidentally end up colliding with the fixed memory address when we need to load the image.
290 std::string error_msg;
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700291 MemMap::Init();
Ian Rogerse63db272014-07-15 15:36:11 -0700292 image_reservation_.reset(MemMap::MapAnonymous("image reservation",
Ian Rogers13735952014-10-08 12:43:28 -0700293 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
Hiroshi Yamauchi1d6fdaf2016-04-07 11:31:26 -0700294 (size_t)120 * 1024 * 1024, // 120MB
Ian Rogerse63db272014-07-15 15:36:11 -0700295 PROT_NONE,
296 false /* no need for 4gb flag with fixed mmap*/,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000297 false /* not reusing existing reservation */,
Ian Rogerse63db272014-07-15 15:36:11 -0700298 &error_msg));
299 CHECK(image_reservation_.get() != nullptr) << error_msg;
300}
301
302void CommonCompilerTest::UnreserveImageSpace() {
303 image_reservation_.reset();
304}
305
306} // namespace art