blob: 7491173ed6487446baf32275a93daafd2545a1e5 [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
Andreas Gampeb68ed2c2018-06-20 10:39:31 -070019#include <type_traits>
20
Ian Rogersd582fa42014-11-05 23:46:43 -080021#include "arch/instruction_set_features.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_field-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080023#include "art_method-inl.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070024#include "base/callee_save_type.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070025#include "base/enums.h"
David Sehrc431b9d2018-03-02 12:01:51 -080026#include "base/utils.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027#include "class_linker.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010028#include "compiled_method-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080029#include "dex/descriptors_names.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "dex/quick_compiler_callbacks.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080031#include "dex/verification_results.h"
Ian Rogerse63db272014-07-15 15:36:11 -070032#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000033#include "driver/compiler_options.h"
Vladimir Markodc4bcce2018-06-21 16:15:42 +010034#include "jni/java_vm_ext.h"
Ian Rogerse63db272014-07-15 15:36:11 -070035#include "interpreter/interpreter.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070036#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "mirror/class_loader.h"
Ian Rogerse63db272014-07-15 15:36:11 -070038#include "mirror/dex_cache.h"
39#include "mirror/object-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010040#include "oat_quick_method_header.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070041#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070042#include "thread-current-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070043
44namespace art {
45
Ian Rogerse63db272014-07-15 15:36:11 -070046CommonCompilerTest::CommonCompilerTest() {}
47CommonCompilerTest::~CommonCompilerTest() {}
48
Mathieu Chartiere401d142015-04-22 13:56:20 -070049void CommonCompilerTest::MakeExecutable(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -070050 CHECK(method != nullptr);
51
52 const CompiledMethod* compiled_method = nullptr;
53 if (!method->IsAbstract()) {
54 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
55 const DexFile& dex_file = *dex_cache->GetDexFile();
56 compiled_method =
57 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
58 method->GetDexMethodIndex()));
59 }
Calin Juravlee0ac1152017-02-13 19:03:47 -080060 // If the code size is 0 it means the method was skipped due to profile guided compilation.
61 if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0u) {
Vladimir Marko35831e82015-09-11 11:59:18 +010062 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070063 const uint32_t code_size = code.size();
Vladimir Marko35831e82015-09-11 11:59:18 +010064 ArrayRef<const uint8_t> vmap_table = compiled_method->GetVmapTable();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070065 const uint32_t vmap_table_offset = vmap_table.empty() ? 0u
Vladimir Marko35831e82015-09-11 11:59:18 +010066 : sizeof(OatQuickMethodHeader) + vmap_table.size();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070067 // The method info is directly before the vmap table.
68 ArrayRef<const uint8_t> method_info = compiled_method->GetMethodInfo();
69 const uint32_t method_info_offset = method_info.empty() ? 0u
70 : vmap_table_offset + method_info.size();
71
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010072 OatQuickMethodHeader method_header(vmap_table_offset,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070073 method_info_offset,
Elliott Hughes956af0f2014-12-11 14:34:28 -080074 compiled_method->GetFrameSizeInBytes(),
75 compiled_method->GetCoreSpillMask(),
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010076 compiled_method->GetFpSpillMask(),
77 code_size);
Ian Rogerse63db272014-07-15 15:36:11 -070078
Elliott Hughes956af0f2014-12-11 14:34:28 -080079 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
80 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
Vladimir Marko562ff442015-10-27 18:51:20 +000081 const size_t max_padding = GetInstructionSetAlignment(compiled_method->GetInstructionSet());
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070082 const size_t size = method_info.size() + vmap_table.size() + sizeof(method_header) + code_size;
Vladimir Marko562ff442015-10-27 18:51:20 +000083 chunk->reserve(size + max_padding);
Elliott Hughes956af0f2014-12-11 14:34:28 -080084 chunk->resize(sizeof(method_header));
Andreas Gampeb68ed2c2018-06-20 10:39:31 -070085 static_assert(std::is_trivially_copyable<OatQuickMethodHeader>::value, "Cannot use memcpy");
Elliott Hughes956af0f2014-12-11 14:34:28 -080086 memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
Vladimir Marko35831e82015-09-11 11:59:18 +010087 chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end());
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070088 chunk->insert(chunk->begin(), method_info.begin(), method_info.end());
Vladimir Marko35831e82015-09-11 11:59:18 +010089 chunk->insert(chunk->end(), code.begin(), code.end());
Vladimir Marko562ff442015-10-27 18:51:20 +000090 CHECK_EQ(chunk->size(), size);
91 const void* unaligned_code_ptr = chunk->data() + (size - code_size);
92 size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr));
93 size_t padding = compiled_method->AlignCode(offset) - offset;
94 // Make sure no resizing takes place.
95 CHECK_GE(chunk->capacity(), chunk->size() + padding);
96 chunk->insert(chunk->begin(), padding, 0);
97 const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding;
98 CHECK_EQ(code_ptr, static_cast<const void*>(chunk->data() + (chunk->size() - code_size)));
Vladimir Marko35831e82015-09-11 11:59:18 +010099 MakeExecutable(code_ptr, code.size());
Ian Rogerse63db272014-07-15 15:36:11 -0700100 const void* method_code = CompiledMethod::CodePointer(code_ptr,
101 compiled_method->GetInstructionSet());
David Sehr709b0702016-10-13 09:12:37 -0700102 LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code;
Vladimir Markofbfc3942017-07-27 16:51:35 +0100103 method->SetEntryPointFromQuickCompiledCode(method_code);
Ian Rogerse63db272014-07-15 15:36:11 -0700104 } else {
105 // No code? You must mean to go into the interpreter.
106 // Or the generic JNI...
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700107 class_linker_->SetEntryPointsToInterpreter(method);
Ian Rogerse63db272014-07-15 15:36:11 -0700108 }
109}
110
111void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
112 CHECK(code_start != nullptr);
113 CHECK_NE(code_length, 0U);
114 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
115 uintptr_t base = RoundDown(data, kPageSize);
116 uintptr_t limit = RoundUp(data + code_length, kPageSize);
117 uintptr_t len = limit - base;
118 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
119 CHECK_EQ(result, 0);
120
Roland Levillain32430262016-02-01 15:23:20 +0000121 FlushInstructionCache(reinterpret_cast<char*>(base), reinterpret_cast<char*>(base + len));
Ian Rogerse63db272014-07-15 15:36:11 -0700122}
123
Mathieu Chartier0795f232016-09-27 18:43:30 -0700124void CommonCompilerTest::MakeExecutable(ObjPtr<mirror::ClassLoader> class_loader,
125 const char* class_name) {
Ian Rogerse63db272014-07-15 15:36:11 -0700126 std::string class_descriptor(DotToDescriptor(class_name));
127 Thread* self = Thread::Current();
128 StackHandleScope<1> hs(self);
129 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +0100130 ObjPtr<mirror::Class> klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700131 CHECK(klass != nullptr) << "Class not found " << class_name;
Andreas Gampe542451c2016-07-26 09:02:02 -0700132 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800133 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134 MakeExecutable(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700135 }
136}
137
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100138// Get the set of image classes given to the compiler options in SetUp.
Vladimir Marko54159c62018-06-20 14:30:08 +0100139std::unique_ptr<HashSet<std::string>> CommonCompilerTest::GetImageClasses() {
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700140 // Empty set: by default no classes are retained in the image.
Vladimir Marko54159c62018-06-20 14:30:08 +0100141 return std::make_unique<HashSet<std::string>>();
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700142}
143
Calin Juravle877fd962016-01-05 14:29:29 +0000144// Get ProfileCompilationInfo that should be passed to the driver.
145ProfileCompilationInfo* CommonCompilerTest::GetProfileCompilationInfo() {
146 // Null, profile information will not be taken into account.
147 return nullptr;
148}
149
Ian Rogerse63db272014-07-15 15:36:11 -0700150void CommonCompilerTest::SetUp() {
151 CommonRuntimeTest::SetUp();
152 {
153 ScopedObjectAccess soa(Thread::Current());
154
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700155 const InstructionSet instruction_set = kRuntimeISA;
Ian Rogerse63db272014-07-15 15:36:11 -0700156 // Take the default set of instruction features from the build.
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800157 instruction_set_features_ = InstructionSetFeatures::FromCppDefines();
Ian Rogerse63db272014-07-15 15:36:11 -0700158
159 runtime_->SetInstructionSet(instruction_set);
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700160 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); ++i) {
161 CalleeSaveType type = CalleeSaveType(i);
Ian Rogerse63db272014-07-15 15:36:11 -0700162 if (!runtime_->HasCalleeSaveMethod(type)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700163 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
Ian Rogerse63db272014-07-15 15:36:11 -0700164 }
165 }
166
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800167 CreateCompilerDriver(compiler_kind_, instruction_set);
Ian Rogerse63db272014-07-15 15:36:11 -0700168 }
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800169}
170
Andreas Gampe3f41a012016-02-18 16:53:41 -0800171void CommonCompilerTest::CreateCompilerDriver(Compiler::Kind kind,
172 InstructionSet isa,
173 size_t number_of_threads) {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000174 compiler_options_->boot_image_ = true;
Mathieu Chartierd0af56c2017-02-17 12:56:25 -0800175 compiler_options_->SetCompilerFilter(GetCompilerFilter());
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100176 compiler_options_->image_classes_.swap(*GetImageClasses());
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800177 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
178 verification_results_.get(),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800179 kind,
180 isa,
181 instruction_set_features_.get(),
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100182 &compiler_options_->image_classes_,
Andreas Gampe3f41a012016-02-18 16:53:41 -0800183 number_of_threads,
Vladimir Marko944da602016-02-19 12:27:55 +0000184 /* swap_fd */ -1,
Calin Juravle877fd962016-01-05 14:29:29 +0000185 GetProfileCompilationInfo()));
Ian Rogerse63db272014-07-15 15:36:11 -0700186 // We typically don't generate an image in unit tests, disable this optimization by default.
187 compiler_driver_->SetSupportBootImageFixup(false);
188}
189
190void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
191 CommonRuntimeTest::SetUpRuntimeOptions(options);
192
193 compiler_options_.reset(new CompilerOptions);
194 verification_results_.reset(new VerificationResults(compiler_options_.get()));
Mathieu Chartiere01b6f62017-07-19 16:55:04 -0700195 QuickCompilerCallbacks* callbacks =
196 new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp);
197 callbacks->SetVerificationResults(verification_results_.get());
198 callbacks_.reset(callbacks);
Ian Rogerse63db272014-07-15 15:36:11 -0700199}
200
Roland Levillainbbcc01a2015-06-30 14:16:48 +0100201Compiler::Kind CommonCompilerTest::GetCompilerKind() const {
202 return compiler_kind_;
203}
204
205void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) {
206 compiler_kind_ = compiler_kind;
207}
208
Roland Levillain0d5a2812015-11-13 10:07:31 +0000209InstructionSet CommonCompilerTest::GetInstructionSet() const {
210 DCHECK(compiler_driver_.get() != nullptr);
211 return compiler_driver_->GetInstructionSet();
212}
213
Ian Rogerse63db272014-07-15 15:36:11 -0700214void CommonCompilerTest::TearDown() {
Ian Rogerse63db272014-07-15 15:36:11 -0700215 compiler_driver_.reset();
216 callbacks_.reset();
Ian Rogerse63db272014-07-15 15:36:11 -0700217 verification_results_.reset();
218 compiler_options_.reset();
Mathieu Chartier496577f2016-09-20 15:33:31 -0700219 image_reservation_.reset();
Ian Rogerse63db272014-07-15 15:36:11 -0700220
221 CommonRuntimeTest::TearDown();
222}
223
224void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
225 std::string class_descriptor(DotToDescriptor(class_name));
226 Thread* self = Thread::Current();
227 StackHandleScope<1> hs(self);
228 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +0100229 ObjPtr<mirror::Class> klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700230 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231 auto pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800232 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700233 CompileMethod(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700234 }
235}
236
Mathieu Chartiere401d142015-04-22 13:56:20 -0700237void CommonCompilerTest::CompileMethod(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -0700238 CHECK(method != nullptr);
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100239 TimingLogger timings("CommonCompilerTest::CompileMethod", false, false);
Ian Rogerse63db272014-07-15 15:36:11 -0700240 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100241 {
242 Thread* self = Thread::Current();
243 jobject class_loader = self->GetJniEnv()->GetVm()->AddGlobalRef(self, method->GetClassLoader());
244
245 DCHECK(!Runtime::Current()->IsStarted());
246 const DexFile* dex_file = method->GetDexFile();
247 uint16_t class_def_idx = method->GetClassDefIndex();
248 uint32_t method_idx = method->GetDexMethodIndex();
249 uint32_t access_flags = method->GetAccessFlags();
250 InvokeType invoke_type = method->GetInvokeType();
251 StackHandleScope<2> hs(self);
252 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
253 Handle<mirror::ClassLoader> h_class_loader = hs.NewHandle(
254 self->DecodeJObject(class_loader)->AsClassLoader());
255 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
256
257 std::vector<const DexFile*> dex_files;
258 dex_files.push_back(dex_file);
259
260 // Go to native so that we don't block GC during compilation.
261 ScopedThreadSuspension sts(self, kNative);
262
263 compiler_driver_->InitializeThreadPools();
264
265 compiler_driver_->PreCompile(class_loader, dex_files, &timings);
266
267 compiler_driver_->CompileOne(self,
268 class_loader,
269 *dex_file,
270 class_def_idx,
271 method_idx,
272 access_flags,
273 invoke_type,
274 code_item,
275 dex_cache,
276 h_class_loader);
277
278 compiler_driver_->FreeThreadPools();
279
280 self->GetJniEnv()->DeleteGlobalRef(class_loader);
281 }
Ian Rogerse63db272014-07-15 15:36:11 -0700282 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
283 MakeExecutable(method);
284}
285
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700286void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
Ian Rogerse63db272014-07-15 15:36:11 -0700287 const char* class_name, const char* method_name,
288 const char* signature) {
289 std::string class_descriptor(DotToDescriptor(class_name));
290 Thread* self = Thread::Current();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100291 ObjPtr<mirror::Class> klass =
292 class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700293 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700294 auto pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100295 ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size);
296 CHECK(method != nullptr && method->IsDirect()) << "Direct method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700297 << class_name << "." << method_name << signature;
298 CompileMethod(method);
299}
300
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700301void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700302 const char* class_name, const char* method_name,
303 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700304 std::string class_descriptor(DotToDescriptor(class_name));
305 Thread* self = Thread::Current();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100306 ObjPtr<mirror::Class> klass =
307 class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700308 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 auto pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100310 ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size);
311 CHECK(method != nullptr && !method->IsDirect()) << "Virtual method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700312 << class_name << "." << method_name << signature;
313 CompileMethod(method);
314}
315
316void CommonCompilerTest::ReserveImageSpace() {
317 // Reserve where the image will be loaded up front so that other parts of test set up don't
318 // accidentally end up colliding with the fixed memory address when we need to load the image.
319 std::string error_msg;
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700320 MemMap::Init();
Ian Rogerse63db272014-07-15 15:36:11 -0700321 image_reservation_.reset(MemMap::MapAnonymous("image reservation",
Ian Rogers13735952014-10-08 12:43:28 -0700322 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
Hiroshi Yamauchi1d6fdaf2016-04-07 11:31:26 -0700323 (size_t)120 * 1024 * 1024, // 120MB
Ian Rogerse63db272014-07-15 15:36:11 -0700324 PROT_NONE,
325 false /* no need for 4gb flag with fixed mmap*/,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000326 false /* not reusing existing reservation */,
Ian Rogerse63db272014-07-15 15:36:11 -0700327 &error_msg));
328 CHECK(image_reservation_.get() != nullptr) << error_msg;
329}
330
331void CommonCompilerTest::UnreserveImageSpace() {
332 image_reservation_.reset();
333}
334
335} // namespace art