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