Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | b68ed2c | 2018-06-20 10:39:31 -0700 | [diff] [blame] | 19 | #include <type_traits> |
| 20 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 21 | #include "arch/instruction_set_features.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 24 | #include "base/callee_save_type.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 25 | #include "base/enums.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 26 | #include "base/utils.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 27 | #include "class_linker.h" |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 28 | #include "compiled_method-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 29 | #include "dex/descriptors_names.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 30 | #include "dex/quick_compiler_callbacks.h" |
Mathieu Chartier | 5bdab12 | 2015-01-26 18:30:19 -0800 | [diff] [blame] | 31 | #include "dex/verification_results.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 32 | #include "driver/compiler_driver.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 33 | #include "driver/compiler_options.h" |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 34 | #include "jni/java_vm_ext.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 35 | #include "interpreter/interpreter.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 36 | #include "mirror/class-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 37 | #include "mirror/class_loader.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 38 | #include "mirror/dex_cache.h" |
| 39 | #include "mirror/object-inl.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 40 | #include "oat_quick_method_header.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 41 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 42 | #include "thread-current-inl.h" |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame^] | 43 | #include "utils/atomic_dex_ref_map-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 44 | |
| 45 | namespace art { |
| 46 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 47 | CommonCompilerTest::CommonCompilerTest() {} |
| 48 | CommonCompilerTest::~CommonCompilerTest() {} |
| 49 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 50 | void CommonCompilerTest::MakeExecutable(ArtMethod* method) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 51 | CHECK(method != nullptr); |
| 52 | |
| 53 | const CompiledMethod* compiled_method = nullptr; |
| 54 | if (!method->IsAbstract()) { |
| 55 | mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache(); |
| 56 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 57 | compiled_method = |
| 58 | compiler_driver_->GetCompiledMethod(MethodReference(&dex_file, |
| 59 | method->GetDexMethodIndex())); |
| 60 | } |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 61 | // If the code size is 0 it means the method was skipped due to profile guided compilation. |
| 62 | if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0u) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 63 | ArrayRef<const uint8_t> code = compiled_method->GetQuickCode(); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 64 | const uint32_t code_size = code.size(); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 65 | ArrayRef<const uint8_t> vmap_table = compiled_method->GetVmapTable(); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 66 | const uint32_t vmap_table_offset = vmap_table.empty() ? 0u |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 67 | : sizeof(OatQuickMethodHeader) + vmap_table.size(); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 68 | // The method info is directly before the vmap table. |
| 69 | ArrayRef<const uint8_t> method_info = compiled_method->GetMethodInfo(); |
| 70 | const uint32_t method_info_offset = method_info.empty() ? 0u |
| 71 | : vmap_table_offset + method_info.size(); |
| 72 | |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 73 | OatQuickMethodHeader method_header(vmap_table_offset, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 74 | method_info_offset, |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 75 | compiled_method->GetFrameSizeInBytes(), |
| 76 | compiled_method->GetCoreSpillMask(), |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 77 | compiled_method->GetFpSpillMask(), |
| 78 | code_size); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 79 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 80 | header_code_and_maps_chunks_.push_back(std::vector<uint8_t>()); |
| 81 | std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back(); |
Vladimir Marko | 562ff44 | 2015-10-27 18:51:20 +0000 | [diff] [blame] | 82 | const size_t max_padding = GetInstructionSetAlignment(compiled_method->GetInstructionSet()); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 83 | const size_t size = method_info.size() + vmap_table.size() + sizeof(method_header) + code_size; |
Vladimir Marko | 562ff44 | 2015-10-27 18:51:20 +0000 | [diff] [blame] | 84 | chunk->reserve(size + max_padding); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 85 | chunk->resize(sizeof(method_header)); |
Andreas Gampe | b68ed2c | 2018-06-20 10:39:31 -0700 | [diff] [blame] | 86 | static_assert(std::is_trivially_copyable<OatQuickMethodHeader>::value, "Cannot use memcpy"); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 87 | memcpy(&(*chunk)[0], &method_header, sizeof(method_header)); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 88 | chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end()); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 89 | chunk->insert(chunk->begin(), method_info.begin(), method_info.end()); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 90 | chunk->insert(chunk->end(), code.begin(), code.end()); |
Vladimir Marko | 562ff44 | 2015-10-27 18:51:20 +0000 | [diff] [blame] | 91 | CHECK_EQ(chunk->size(), size); |
| 92 | const void* unaligned_code_ptr = chunk->data() + (size - code_size); |
| 93 | size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr)); |
| 94 | size_t padding = compiled_method->AlignCode(offset) - offset; |
| 95 | // Make sure no resizing takes place. |
| 96 | CHECK_GE(chunk->capacity(), chunk->size() + padding); |
| 97 | chunk->insert(chunk->begin(), padding, 0); |
| 98 | const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding; |
| 99 | CHECK_EQ(code_ptr, static_cast<const void*>(chunk->data() + (chunk->size() - code_size))); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 100 | MakeExecutable(code_ptr, code.size()); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 101 | const void* method_code = CompiledMethod::CodePointer(code_ptr, |
| 102 | compiled_method->GetInstructionSet()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 103 | LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code; |
Vladimir Marko | fbfc394 | 2017-07-27 16:51:35 +0100 | [diff] [blame] | 104 | method->SetEntryPointFromQuickCompiledCode(method_code); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 105 | } else { |
| 106 | // No code? You must mean to go into the interpreter. |
| 107 | // Or the generic JNI... |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 108 | class_linker_->SetEntryPointsToInterpreter(method); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) { |
| 113 | CHECK(code_start != nullptr); |
| 114 | CHECK_NE(code_length, 0U); |
| 115 | uintptr_t data = reinterpret_cast<uintptr_t>(code_start); |
| 116 | uintptr_t base = RoundDown(data, kPageSize); |
| 117 | uintptr_t limit = RoundUp(data + code_length, kPageSize); |
| 118 | uintptr_t len = limit - base; |
| 119 | int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC); |
| 120 | CHECK_EQ(result, 0); |
| 121 | |
Roland Levillain | 3243026 | 2016-02-01 15:23:20 +0000 | [diff] [blame] | 122 | FlushInstructionCache(reinterpret_cast<char*>(base), reinterpret_cast<char*>(base + len)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 125 | void CommonCompilerTest::MakeExecutable(ObjPtr<mirror::ClassLoader> class_loader, |
| 126 | const char* class_name) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 127 | std::string class_descriptor(DotToDescriptor(class_name)); |
| 128 | Thread* self = Thread::Current(); |
| 129 | StackHandleScope<1> hs(self); |
| 130 | Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader)); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 131 | ObjPtr<mirror::Class> klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 132 | CHECK(klass != nullptr) << "Class not found " << class_name; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 133 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 134 | for (auto& m : klass->GetMethods(pointer_size)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 135 | MakeExecutable(&m); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 139 | // Get the set of image classes given to the compiler options in SetUp. |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 140 | std::unique_ptr<HashSet<std::string>> CommonCompilerTest::GetImageClasses() { |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 141 | // Empty set: by default no classes are retained in the image. |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 142 | return std::make_unique<HashSet<std::string>>(); |
Andreas Gampe | 70bef0d | 2015-04-15 02:37:28 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 145 | // Get ProfileCompilationInfo that should be passed to the driver. |
| 146 | ProfileCompilationInfo* CommonCompilerTest::GetProfileCompilationInfo() { |
| 147 | // Null, profile information will not be taken into account. |
| 148 | return nullptr; |
| 149 | } |
| 150 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 151 | void CommonCompilerTest::SetUp() { |
| 152 | CommonRuntimeTest::SetUp(); |
| 153 | { |
| 154 | ScopedObjectAccess soa(Thread::Current()); |
| 155 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 156 | const InstructionSet instruction_set = kRuntimeISA; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 157 | // Take the default set of instruction features from the build. |
Andreas Gampe | 0415b4e | 2015-01-06 15:17:07 -0800 | [diff] [blame] | 158 | instruction_set_features_ = InstructionSetFeatures::FromCppDefines(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 159 | |
| 160 | runtime_->SetInstructionSet(instruction_set); |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 161 | for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); ++i) { |
| 162 | CalleeSaveType type = CalleeSaveType(i); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 163 | if (!runtime_->HasCalleeSaveMethod(type)) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 164 | runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 168 | CreateCompilerDriver(compiler_kind_, instruction_set); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 169 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Andreas Gampe | 3f41a01 | 2016-02-18 16:53:41 -0800 | [diff] [blame] | 172 | void CommonCompilerTest::CreateCompilerDriver(Compiler::Kind kind, |
| 173 | InstructionSet isa, |
| 174 | size_t number_of_threads) { |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 175 | compiler_options_->boot_image_ = true; |
Mathieu Chartier | d0af56c | 2017-02-17 12:56:25 -0800 | [diff] [blame] | 176 | compiler_options_->SetCompilerFilter(GetCompilerFilter()); |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 177 | compiler_options_->image_classes_.swap(*GetImageClasses()); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 178 | compiler_driver_.reset(new CompilerDriver(compiler_options_.get(), |
| 179 | verification_results_.get(), |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 180 | kind, |
| 181 | isa, |
| 182 | instruction_set_features_.get(), |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 183 | &compiler_options_->image_classes_, |
Andreas Gampe | 3f41a01 | 2016-02-18 16:53:41 -0800 | [diff] [blame] | 184 | number_of_threads, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 185 | /* swap_fd */ -1, |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 186 | GetProfileCompilationInfo())); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 187 | // We typically don't generate an image in unit tests, disable this optimization by default. |
| 188 | compiler_driver_->SetSupportBootImageFixup(false); |
| 189 | } |
| 190 | |
| 191 | void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) { |
| 192 | CommonRuntimeTest::SetUpRuntimeOptions(options); |
| 193 | |
| 194 | compiler_options_.reset(new CompilerOptions); |
| 195 | verification_results_.reset(new VerificationResults(compiler_options_.get())); |
Mathieu Chartier | e01b6f6 | 2017-07-19 16:55:04 -0700 | [diff] [blame] | 196 | QuickCompilerCallbacks* callbacks = |
| 197 | new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp); |
| 198 | callbacks->SetVerificationResults(verification_results_.get()); |
| 199 | callbacks_.reset(callbacks); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Roland Levillain | bbcc01a | 2015-06-30 14:16:48 +0100 | [diff] [blame] | 202 | Compiler::Kind CommonCompilerTest::GetCompilerKind() const { |
| 203 | return compiler_kind_; |
| 204 | } |
| 205 | |
| 206 | void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) { |
| 207 | compiler_kind_ = compiler_kind; |
| 208 | } |
| 209 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 210 | InstructionSet CommonCompilerTest::GetInstructionSet() const { |
| 211 | DCHECK(compiler_driver_.get() != nullptr); |
| 212 | return compiler_driver_->GetInstructionSet(); |
| 213 | } |
| 214 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 215 | void CommonCompilerTest::TearDown() { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 216 | compiler_driver_.reset(); |
| 217 | callbacks_.reset(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 218 | verification_results_.reset(); |
| 219 | compiler_options_.reset(); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 220 | image_reservation_.reset(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 221 | |
| 222 | CommonRuntimeTest::TearDown(); |
| 223 | } |
| 224 | |
| 225 | void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) { |
| 226 | std::string class_descriptor(DotToDescriptor(class_name)); |
| 227 | Thread* self = Thread::Current(); |
| 228 | StackHandleScope<1> hs(self); |
| 229 | Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader)); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 230 | ObjPtr<mirror::Class> klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 231 | CHECK(klass != nullptr) << "Class not found " << class_name; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 232 | auto pointer_size = class_linker_->GetImagePointerSize(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 233 | for (auto& m : klass->GetMethods(pointer_size)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 234 | CompileMethod(&m); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 238 | void CommonCompilerTest::CompileMethod(ArtMethod* method) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 239 | CHECK(method != nullptr); |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 240 | TimingLogger timings("CommonCompilerTest::CompileMethod", false, false); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 241 | TimingLogger::ScopedTiming t(__FUNCTION__, &timings); |
Vladimir Marko | dc4bcce | 2018-06-21 16:15:42 +0100 | [diff] [blame] | 242 | { |
| 243 | Thread* self = Thread::Current(); |
| 244 | jobject class_loader = self->GetJniEnv()->GetVm()->AddGlobalRef(self, method->GetClassLoader()); |
| 245 | |
| 246 | DCHECK(!Runtime::Current()->IsStarted()); |
| 247 | const DexFile* dex_file = method->GetDexFile(); |
| 248 | uint16_t class_def_idx = method->GetClassDefIndex(); |
| 249 | uint32_t method_idx = method->GetDexMethodIndex(); |
| 250 | uint32_t access_flags = method->GetAccessFlags(); |
| 251 | InvokeType invoke_type = method->GetInvokeType(); |
| 252 | StackHandleScope<2> hs(self); |
| 253 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); |
| 254 | Handle<mirror::ClassLoader> h_class_loader = hs.NewHandle( |
| 255 | self->DecodeJObject(class_loader)->AsClassLoader()); |
| 256 | const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); |
| 257 | |
| 258 | std::vector<const DexFile*> dex_files; |
| 259 | dex_files.push_back(dex_file); |
| 260 | |
| 261 | // Go to native so that we don't block GC during compilation. |
| 262 | ScopedThreadSuspension sts(self, kNative); |
| 263 | |
| 264 | compiler_driver_->InitializeThreadPools(); |
| 265 | |
| 266 | compiler_driver_->PreCompile(class_loader, dex_files, &timings); |
| 267 | |
| 268 | compiler_driver_->CompileOne(self, |
| 269 | class_loader, |
| 270 | *dex_file, |
| 271 | class_def_idx, |
| 272 | method_idx, |
| 273 | access_flags, |
| 274 | invoke_type, |
| 275 | code_item, |
| 276 | dex_cache, |
| 277 | h_class_loader); |
| 278 | |
| 279 | compiler_driver_->FreeThreadPools(); |
| 280 | |
| 281 | self->GetJniEnv()->DeleteGlobalRef(class_loader); |
| 282 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 283 | TimingLogger::ScopedTiming t2("MakeExecutable", &timings); |
| 284 | MakeExecutable(method); |
| 285 | } |
| 286 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 287 | void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader, |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 288 | const char* class_name, const char* method_name, |
| 289 | const char* signature) { |
| 290 | std::string class_descriptor(DotToDescriptor(class_name)); |
| 291 | Thread* self = Thread::Current(); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 292 | ObjPtr<mirror::Class> klass = |
| 293 | class_linker_->FindClass(self, class_descriptor.c_str(), class_loader); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 294 | CHECK(klass != nullptr) << "Class not found " << class_name; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 295 | auto pointer_size = class_linker_->GetImagePointerSize(); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 296 | ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size); |
| 297 | CHECK(method != nullptr && method->IsDirect()) << "Direct method not found: " |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 298 | << class_name << "." << method_name << signature; |
| 299 | CompileMethod(method); |
| 300 | } |
| 301 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 302 | void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 303 | const char* class_name, const char* method_name, |
| 304 | const char* signature) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 305 | std::string class_descriptor(DotToDescriptor(class_name)); |
| 306 | Thread* self = Thread::Current(); |
Vladimir Marko | e9987b0 | 2018-05-22 16:26:43 +0100 | [diff] [blame] | 307 | ObjPtr<mirror::Class> klass = |
| 308 | class_linker_->FindClass(self, class_descriptor.c_str(), class_loader); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 309 | CHECK(klass != nullptr) << "Class not found " << class_name; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 310 | auto pointer_size = class_linker_->GetImagePointerSize(); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 311 | ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size); |
| 312 | CHECK(method != nullptr && !method->IsDirect()) << "Virtual method not found: " |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 313 | << class_name << "." << method_name << signature; |
| 314 | CompileMethod(method); |
| 315 | } |
| 316 | |
| 317 | void CommonCompilerTest::ReserveImageSpace() { |
| 318 | // Reserve where the image will be loaded up front so that other parts of test set up don't |
| 319 | // accidentally end up colliding with the fixed memory address when we need to load the image. |
| 320 | std::string error_msg; |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 321 | MemMap::Init(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 322 | image_reservation_.reset(MemMap::MapAnonymous("image reservation", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 323 | reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS), |
Hiroshi Yamauchi | 1d6fdaf | 2016-04-07 11:31:26 -0700 | [diff] [blame] | 324 | (size_t)120 * 1024 * 1024, // 120MB |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 325 | PROT_NONE, |
| 326 | false /* no need for 4gb flag with fixed mmap*/, |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 327 | false /* not reusing existing reservation */, |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 328 | &error_msg)); |
| 329 | CHECK(image_reservation_.get() != nullptr) << error_msg; |
| 330 | } |
| 331 | |
| 332 | void CommonCompilerTest::UnreserveImageSpace() { |
| 333 | image_reservation_.reset(); |
| 334 | } |
| 335 | |
Vladimir Marko | 213ee2d | 2018-06-22 11:56:34 +0100 | [diff] [blame^] | 336 | void CommonCompilerTest::SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files) { |
| 337 | compiler_options_->dex_files_for_oat_file_ = dex_files; |
| 338 | compiler_driver_->compiled_classes_.AddDexFiles(dex_files); |
| 339 | compiler_driver_->dex_to_dex_compiler_.SetDexFiles(dex_files); |
| 340 | } |
| 341 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 342 | } // namespace art |