blob: a44b9ae523c52c070e89a3b950743ab12921e21f [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"
Vladimir Marko2afaff72018-11-30 17:01:50 +000025#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
David Sehrc431b9d2018-03-02 12:01:51 -080027#include "base/utils.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "class_linker.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010029#include "compiled_method-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080030#include "dex/descriptors_names.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080031#include "dex/verification_results.h"
Vladimir Marko815d5e52019-03-04 10:18:31 +000032#include "driver/compiled_method_storage.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"
Vladimir Marko213ee2d2018-06-22 11:56:34 +010043#include "utils/atomic_dex_ref_map-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070044
45namespace art {
46
Ian Rogerse63db272014-07-15 15:36:11 -070047CommonCompilerTest::CommonCompilerTest() {}
48CommonCompilerTest::~CommonCompilerTest() {}
49
Vladimir Marko815d5e52019-03-04 10:18:31 +000050void CommonCompilerTest::MakeExecutable(ArtMethod* method, const CompiledMethod* compiled_method) {
Ian Rogerse63db272014-07-15 15:36:11 -070051 CHECK(method != nullptr);
Calin Juravlee0ac1152017-02-13 19:03:47 -080052 // If the code size is 0 it means the method was skipped due to profile guided compilation.
53 if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0u) {
Vladimir Marko35831e82015-09-11 11:59:18 +010054 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070055 const uint32_t code_size = code.size();
Vladimir Marko35831e82015-09-11 11:59:18 +010056 ArrayRef<const uint8_t> vmap_table = compiled_method->GetVmapTable();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070057 const uint32_t vmap_table_offset = vmap_table.empty() ? 0u
Vladimir Marko35831e82015-09-11 11:59:18 +010058 : sizeof(OatQuickMethodHeader) + vmap_table.size();
Nicolas Geoffray57083762019-03-05 09:24:45 +000059 OatQuickMethodHeader method_header(vmap_table_offset, code_size);
Ian Rogerse63db272014-07-15 15:36:11 -070060
Elliott Hughes956af0f2014-12-11 14:34:28 -080061 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
62 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
Vladimir Marko562ff442015-10-27 18:51:20 +000063 const size_t max_padding = GetInstructionSetAlignment(compiled_method->GetInstructionSet());
David Srbecky8cd54542018-07-15 23:58:44 +010064 const size_t size = vmap_table.size() + sizeof(method_header) + code_size;
Vladimir Marko562ff442015-10-27 18:51:20 +000065 chunk->reserve(size + max_padding);
Elliott Hughes956af0f2014-12-11 14:34:28 -080066 chunk->resize(sizeof(method_header));
Andreas Gampeb68ed2c2018-06-20 10:39:31 -070067 static_assert(std::is_trivially_copyable<OatQuickMethodHeader>::value, "Cannot use memcpy");
Elliott Hughes956af0f2014-12-11 14:34:28 -080068 memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
Vladimir Marko35831e82015-09-11 11:59:18 +010069 chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end());
Vladimir Marko35831e82015-09-11 11:59:18 +010070 chunk->insert(chunk->end(), code.begin(), code.end());
Vladimir Marko562ff442015-10-27 18:51:20 +000071 CHECK_EQ(chunk->size(), size);
72 const void* unaligned_code_ptr = chunk->data() + (size - code_size);
73 size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr));
74 size_t padding = compiled_method->AlignCode(offset) - offset;
75 // Make sure no resizing takes place.
76 CHECK_GE(chunk->capacity(), chunk->size() + padding);
77 chunk->insert(chunk->begin(), padding, 0);
78 const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding;
79 CHECK_EQ(code_ptr, static_cast<const void*>(chunk->data() + (chunk->size() - code_size)));
Vladimir Marko35831e82015-09-11 11:59:18 +010080 MakeExecutable(code_ptr, code.size());
Ian Rogerse63db272014-07-15 15:36:11 -070081 const void* method_code = CompiledMethod::CodePointer(code_ptr,
82 compiled_method->GetInstructionSet());
David Sehr709b0702016-10-13 09:12:37 -070083 LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code;
Vladimir Markofbfc3942017-07-27 16:51:35 +010084 method->SetEntryPointFromQuickCompiledCode(method_code);
Ian Rogerse63db272014-07-15 15:36:11 -070085 } else {
86 // No code? You must mean to go into the interpreter.
87 // Or the generic JNI...
Ian Rogers6f3dbba2014-10-14 17:41:57 -070088 class_linker_->SetEntryPointsToInterpreter(method);
Ian Rogerse63db272014-07-15 15:36:11 -070089 }
90}
91
92void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
93 CHECK(code_start != nullptr);
94 CHECK_NE(code_length, 0U);
95 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
96 uintptr_t base = RoundDown(data, kPageSize);
97 uintptr_t limit = RoundUp(data + code_length, kPageSize);
98 uintptr_t len = limit - base;
99 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
100 CHECK_EQ(result, 0);
101
Orion Hodson38d29fd2018-09-07 12:58:37 +0100102 FlushInstructionCache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len));
Ian Rogerse63db272014-07-15 15:36:11 -0700103}
104
Ian Rogerse63db272014-07-15 15:36:11 -0700105void CommonCompilerTest::SetUp() {
106 CommonRuntimeTest::SetUp();
107 {
108 ScopedObjectAccess soa(Thread::Current());
109
Vladimir Markoa0431112018-06-25 09:32:54 +0100110 runtime_->SetInstructionSet(instruction_set_);
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700111 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); ++i) {
112 CalleeSaveType type = CalleeSaveType(i);
Ian Rogerse63db272014-07-15 15:36:11 -0700113 if (!runtime_->HasCalleeSaveMethod(type)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700114 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
Ian Rogerse63db272014-07-15 15:36:11 -0700115 }
116 }
Ian Rogerse63db272014-07-15 15:36:11 -0700117 }
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800118}
119
Vladimir Markoa0431112018-06-25 09:32:54 +0100120void CommonCompilerTest::ApplyInstructionSet() {
121 // Copy local instruction_set_ and instruction_set_features_ to *compiler_options_;
122 CHECK(instruction_set_features_ != nullptr);
123 if (instruction_set_ == InstructionSet::kThumb2) {
124 CHECK_EQ(InstructionSet::kArm, instruction_set_features_->GetInstructionSet());
125 } else {
126 CHECK_EQ(instruction_set_, instruction_set_features_->GetInstructionSet());
127 }
128 compiler_options_->instruction_set_ = instruction_set_;
129 compiler_options_->instruction_set_features_ =
130 InstructionSetFeatures::FromBitmap(instruction_set_, instruction_set_features_->AsBitmap());
131 CHECK(compiler_options_->instruction_set_features_->Equals(instruction_set_features_.get()));
132}
133
134void CommonCompilerTest::OverrideInstructionSetFeatures(InstructionSet instruction_set,
135 const std::string& variant) {
136 instruction_set_ = instruction_set;
137 std::string error_msg;
138 instruction_set_features_ =
139 InstructionSetFeatures::FromVariant(instruction_set, variant, &error_msg);
140 CHECK(instruction_set_features_ != nullptr) << error_msg;
141
142 if (compiler_options_ != nullptr) {
143 ApplyInstructionSet();
144 }
145}
146
Ian Rogerse63db272014-07-15 15:36:11 -0700147void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
148 CommonRuntimeTest::SetUpRuntimeOptions(options);
149
150 compiler_options_.reset(new CompilerOptions);
151 verification_results_.reset(new VerificationResults(compiler_options_.get()));
Vladimir Marko815d5e52019-03-04 10:18:31 +0000152
153 ApplyInstructionSet();
Ian Rogerse63db272014-07-15 15:36:11 -0700154}
155
Roland Levillainbbcc01a2015-06-30 14:16:48 +0100156Compiler::Kind CommonCompilerTest::GetCompilerKind() const {
157 return compiler_kind_;
158}
159
160void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) {
161 compiler_kind_ = compiler_kind;
162}
163
Ian Rogerse63db272014-07-15 15:36:11 -0700164void CommonCompilerTest::TearDown() {
Ian Rogerse63db272014-07-15 15:36:11 -0700165 verification_results_.reset();
166 compiler_options_.reset();
167
168 CommonRuntimeTest::TearDown();
169}
170
Mathieu Chartiere401d142015-04-22 13:56:20 -0700171void CommonCompilerTest::CompileMethod(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -0700172 CHECK(method != nullptr);
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100173 TimingLogger timings("CommonCompilerTest::CompileMethod", false, false);
Ian Rogerse63db272014-07-15 15:36:11 -0700174 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Vladimir Marko815d5e52019-03-04 10:18:31 +0000175 CompiledMethodStorage storage(/*swap_fd=*/ -1);
Vladimir Marko4b2d1c52019-03-06 11:21:11 +0000176 CompiledMethod* compiled_method = nullptr;
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100177 {
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100178 DCHECK(!Runtime::Current()->IsStarted());
Vladimir Marko815d5e52019-03-04 10:18:31 +0000179 Thread* self = Thread::Current();
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100180 StackHandleScope<2> hs(self);
Vladimir Marko815d5e52019-03-04 10:18:31 +0000181 std::unique_ptr<Compiler> compiler(
182 Compiler::Create(*compiler_options_, &storage, compiler_kind_));
183 const DexFile& dex_file = *method->GetDexFile();
184 Handle<mirror::DexCache> dex_cache = hs.NewHandle(class_linker_->FindDexCache(self, dex_file));
185 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(method->GetClassLoader());
Vladimir Marko2afaff72018-11-30 17:01:50 +0000186 compiler_options_->verification_results_ = verification_results_.get();
Vladimir Marko815d5e52019-03-04 10:18:31 +0000187 if (method->IsNative()) {
188 compiled_method = compiler->JniCompile(method->GetAccessFlags(),
189 method->GetDexMethodIndex(),
190 dex_file,
191 dex_cache);
192 } else {
193 verification_results_->AddDexFile(&dex_file);
194 verification_results_->CreateVerifiedMethodFor(
195 MethodReference(&dex_file, method->GetDexMethodIndex()));
196 compiled_method = compiler->Compile(method->GetCodeItem(),
197 method->GetAccessFlags(),
198 method->GetInvokeType(),
199 method->GetClassDefIndex(),
200 method->GetDexMethodIndex(),
201 class_loader,
202 dex_file,
203 dex_cache);
204 }
Vladimir Marko2afaff72018-11-30 17:01:50 +0000205 compiler_options_->verification_results_ = nullptr;
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100206 }
Vladimir Marko4b2d1c52019-03-06 11:21:11 +0000207 CHECK(method != nullptr);
208 {
209 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
210 MakeExecutable(method, compiled_method);
211 }
212 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&storage, compiled_method);
Ian Rogerse63db272014-07-15 15:36:11 -0700213}
214
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700215void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
Ian Rogerse63db272014-07-15 15:36:11 -0700216 const char* class_name, const char* method_name,
217 const char* signature) {
218 std::string class_descriptor(DotToDescriptor(class_name));
219 Thread* self = Thread::Current();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100220 ObjPtr<mirror::Class> klass =
221 class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700222 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700223 auto pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100224 ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size);
225 CHECK(method != nullptr && method->IsDirect()) << "Direct method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700226 << class_name << "." << method_name << signature;
227 CompileMethod(method);
228}
229
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700230void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700231 const char* class_name, const char* method_name,
232 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700233 std::string class_descriptor(DotToDescriptor(class_name));
234 Thread* self = Thread::Current();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100235 ObjPtr<mirror::Class> klass =
236 class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700237 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 auto pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100239 ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size);
240 CHECK(method != nullptr && !method->IsDirect()) << "Virtual method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700241 << class_name << "." << method_name << signature;
242 CompileMethod(method);
243}
244
Vladimir Markoa0431112018-06-25 09:32:54 +0100245void CommonCompilerTest::ClearBootImageOption() {
Vladimir Marko9c4b9702018-11-14 15:09:02 +0000246 compiler_options_->image_type_ = CompilerOptions::ImageType::kNone;
Vladimir Markoa0431112018-06-25 09:32:54 +0100247}
248
Ian Rogerse63db272014-07-15 15:36:11 -0700249} // namespace art