blob: 1d4f0203919e1879b5b02502af9d1cd5d8498b31 [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"
Ian Rogerse63db272014-07-15 15:36:11 -070034#include "interpreter/interpreter.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "mirror/class_loader.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "mirror/dex_cache.h"
38#include "mirror/object-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010039#include "oat_quick_method_header.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070040#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070041#include "thread-current-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042
43namespace art {
44
Ian Rogerse63db272014-07-15 15:36:11 -070045CommonCompilerTest::CommonCompilerTest() {}
46CommonCompilerTest::~CommonCompilerTest() {}
47
Mathieu Chartiere401d142015-04-22 13:56:20 -070048void CommonCompilerTest::MakeExecutable(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -070049 CHECK(method != nullptr);
50
51 const CompiledMethod* compiled_method = nullptr;
52 if (!method->IsAbstract()) {
53 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
54 const DexFile& dex_file = *dex_cache->GetDexFile();
55 compiled_method =
56 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
57 method->GetDexMethodIndex()));
58 }
Calin Juravlee0ac1152017-02-13 19:03:47 -080059 // If the code size is 0 it means the method was skipped due to profile guided compilation.
60 if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0u) {
Vladimir Marko35831e82015-09-11 11:59:18 +010061 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070062 const uint32_t code_size = code.size();
Vladimir Marko35831e82015-09-11 11:59:18 +010063 ArrayRef<const uint8_t> vmap_table = compiled_method->GetVmapTable();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070064 const uint32_t vmap_table_offset = vmap_table.empty() ? 0u
Vladimir Marko35831e82015-09-11 11:59:18 +010065 : sizeof(OatQuickMethodHeader) + vmap_table.size();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070066 // The method info is directly before the vmap table.
67 ArrayRef<const uint8_t> method_info = compiled_method->GetMethodInfo();
68 const uint32_t method_info_offset = method_info.empty() ? 0u
69 : vmap_table_offset + method_info.size();
70
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010071 OatQuickMethodHeader method_header(vmap_table_offset,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070072 method_info_offset,
Elliott Hughes956af0f2014-12-11 14:34:28 -080073 compiled_method->GetFrameSizeInBytes(),
74 compiled_method->GetCoreSpillMask(),
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010075 compiled_method->GetFpSpillMask(),
76 code_size);
Ian Rogerse63db272014-07-15 15:36:11 -070077
Elliott Hughes956af0f2014-12-11 14:34:28 -080078 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
79 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
Vladimir Marko562ff442015-10-27 18:51:20 +000080 const size_t max_padding = GetInstructionSetAlignment(compiled_method->GetInstructionSet());
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070081 const size_t size = method_info.size() + vmap_table.size() + sizeof(method_header) + code_size;
Vladimir Marko562ff442015-10-27 18:51:20 +000082 chunk->reserve(size + max_padding);
Elliott Hughes956af0f2014-12-11 14:34:28 -080083 chunk->resize(sizeof(method_header));
Andreas Gampeb68ed2c2018-06-20 10:39:31 -070084 static_assert(std::is_trivially_copyable<OatQuickMethodHeader>::value, "Cannot use memcpy");
Elliott Hughes956af0f2014-12-11 14:34:28 -080085 memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
Vladimir Marko35831e82015-09-11 11:59:18 +010086 chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end());
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070087 chunk->insert(chunk->begin(), method_info.begin(), method_info.end());
Vladimir Marko35831e82015-09-11 11:59:18 +010088 chunk->insert(chunk->end(), code.begin(), code.end());
Vladimir Marko562ff442015-10-27 18:51:20 +000089 CHECK_EQ(chunk->size(), size);
90 const void* unaligned_code_ptr = chunk->data() + (size - code_size);
91 size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr));
92 size_t padding = compiled_method->AlignCode(offset) - offset;
93 // Make sure no resizing takes place.
94 CHECK_GE(chunk->capacity(), chunk->size() + padding);
95 chunk->insert(chunk->begin(), padding, 0);
96 const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding;
97 CHECK_EQ(code_ptr, static_cast<const void*>(chunk->data() + (chunk->size() - code_size)));
Vladimir Marko35831e82015-09-11 11:59:18 +010098 MakeExecutable(code_ptr, code.size());
Ian Rogerse63db272014-07-15 15:36:11 -070099 const void* method_code = CompiledMethod::CodePointer(code_ptr,
100 compiled_method->GetInstructionSet());
David Sehr709b0702016-10-13 09:12:37 -0700101 LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code;
Vladimir Markofbfc3942017-07-27 16:51:35 +0100102 method->SetEntryPointFromQuickCompiledCode(method_code);
Ian Rogerse63db272014-07-15 15:36:11 -0700103 } else {
104 // No code? You must mean to go into the interpreter.
105 // Or the generic JNI...
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700106 class_linker_->SetEntryPointsToInterpreter(method);
Ian Rogerse63db272014-07-15 15:36:11 -0700107 }
108}
109
110void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
111 CHECK(code_start != nullptr);
112 CHECK_NE(code_length, 0U);
113 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
114 uintptr_t base = RoundDown(data, kPageSize);
115 uintptr_t limit = RoundUp(data + code_length, kPageSize);
116 uintptr_t len = limit - base;
117 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
118 CHECK_EQ(result, 0);
119
Roland Levillain32430262016-02-01 15:23:20 +0000120 FlushInstructionCache(reinterpret_cast<char*>(base), reinterpret_cast<char*>(base + len));
Ian Rogerse63db272014-07-15 15:36:11 -0700121}
122
Mathieu Chartier0795f232016-09-27 18:43:30 -0700123void CommonCompilerTest::MakeExecutable(ObjPtr<mirror::ClassLoader> class_loader,
124 const char* class_name) {
Ian Rogerse63db272014-07-15 15:36:11 -0700125 std::string class_descriptor(DotToDescriptor(class_name));
126 Thread* self = Thread::Current();
127 StackHandleScope<1> hs(self);
128 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +0100129 ObjPtr<mirror::Class> klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700130 CHECK(klass != nullptr) << "Class not found " << class_name;
Andreas Gampe542451c2016-07-26 09:02:02 -0700131 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800132 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700133 MakeExecutable(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700134 }
135}
136
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700137// Get the set of image classes given to the compiler-driver in SetUp. Note: the compiler
138// driver assumes ownership of the set, so the test should properly release the set.
139std::unordered_set<std::string>* CommonCompilerTest::GetImageClasses() {
140 // Empty set: by default no classes are retained in the image.
141 return new std::unordered_set<std::string>();
142}
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());
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800176 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
177 verification_results_.get(),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800178 kind,
179 isa,
180 instruction_set_features_.get(),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800181 GetImageClasses(),
Andreas Gampe3f41a012016-02-18 16:53:41 -0800182 number_of_threads,
Vladimir Marko944da602016-02-19 12:27:55 +0000183 /* swap_fd */ -1,
Calin Juravle877fd962016-01-05 14:29:29 +0000184 GetProfileCompilationInfo()));
Ian Rogerse63db272014-07-15 15:36:11 -0700185 // We typically don't generate an image in unit tests, disable this optimization by default.
186 compiler_driver_->SetSupportBootImageFixup(false);
187}
188
189void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
190 CommonRuntimeTest::SetUpRuntimeOptions(options);
191
192 compiler_options_.reset(new CompilerOptions);
193 verification_results_.reset(new VerificationResults(compiler_options_.get()));
Mathieu Chartiere01b6f62017-07-19 16:55:04 -0700194 QuickCompilerCallbacks* callbacks =
195 new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp);
196 callbacks->SetVerificationResults(verification_results_.get());
197 callbacks_.reset(callbacks);
Ian Rogerse63db272014-07-15 15:36:11 -0700198}
199
Roland Levillainbbcc01a2015-06-30 14:16:48 +0100200Compiler::Kind CommonCompilerTest::GetCompilerKind() const {
201 return compiler_kind_;
202}
203
204void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) {
205 compiler_kind_ = compiler_kind;
206}
207
Roland Levillain0d5a2812015-11-13 10:07:31 +0000208InstructionSet CommonCompilerTest::GetInstructionSet() const {
209 DCHECK(compiler_driver_.get() != nullptr);
210 return compiler_driver_->GetInstructionSet();
211}
212
Ian Rogerse63db272014-07-15 15:36:11 -0700213void CommonCompilerTest::TearDown() {
Ian Rogerse63db272014-07-15 15:36:11 -0700214 compiler_driver_.reset();
215 callbacks_.reset();
Ian Rogerse63db272014-07-15 15:36:11 -0700216 verification_results_.reset();
217 compiler_options_.reset();
Mathieu Chartier496577f2016-09-20 15:33:31 -0700218 image_reservation_.reset();
Ian Rogerse63db272014-07-15 15:36:11 -0700219
220 CommonRuntimeTest::TearDown();
221}
222
223void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
224 std::string class_descriptor(DotToDescriptor(class_name));
225 Thread* self = Thread::Current();
226 StackHandleScope<1> hs(self);
227 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
Vladimir Markoe9987b02018-05-22 16:26:43 +0100228 ObjPtr<mirror::Class> klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700229 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700230 auto pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800231 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700232 CompileMethod(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700233 }
234}
235
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236void CommonCompilerTest::CompileMethod(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -0700237 CHECK(method != nullptr);
238 TimingLogger timings("CommonTest::CompileMethod", false, false);
239 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800240 compiler_driver_->CompileOne(Thread::Current(), method, &timings);
Ian Rogerse63db272014-07-15 15:36:11 -0700241 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
242 MakeExecutable(method);
243}
244
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700245void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
Ian Rogerse63db272014-07-15 15:36:11 -0700246 const char* class_name, const char* method_name,
247 const char* signature) {
248 std::string class_descriptor(DotToDescriptor(class_name));
249 Thread* self = Thread::Current();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100250 ObjPtr<mirror::Class> klass =
251 class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700252 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 auto pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100254 ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size);
255 CHECK(method != nullptr && method->IsDirect()) << "Direct method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700256 << class_name << "." << method_name << signature;
257 CompileMethod(method);
258}
259
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700260void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700261 const char* class_name, const char* method_name,
262 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700263 std::string class_descriptor(DotToDescriptor(class_name));
264 Thread* self = Thread::Current();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100265 ObjPtr<mirror::Class> klass =
266 class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700267 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 auto pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100269 ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size);
270 CHECK(method != nullptr && !method->IsDirect()) << "Virtual method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700271 << class_name << "." << method_name << signature;
272 CompileMethod(method);
273}
274
275void CommonCompilerTest::ReserveImageSpace() {
276 // Reserve where the image will be loaded up front so that other parts of test set up don't
277 // accidentally end up colliding with the fixed memory address when we need to load the image.
278 std::string error_msg;
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700279 MemMap::Init();
Ian Rogerse63db272014-07-15 15:36:11 -0700280 image_reservation_.reset(MemMap::MapAnonymous("image reservation",
Ian Rogers13735952014-10-08 12:43:28 -0700281 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
Hiroshi Yamauchi1d6fdaf2016-04-07 11:31:26 -0700282 (size_t)120 * 1024 * 1024, // 120MB
Ian Rogerse63db272014-07-15 15:36:11 -0700283 PROT_NONE,
284 false /* no need for 4gb flag with fixed mmap*/,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000285 false /* not reusing existing reservation */,
Ian Rogerse63db272014-07-15 15:36:11 -0700286 &error_msg));
287 CHECK(image_reservation_.get() != nullptr) << error_msg;
288}
289
290void CommonCompilerTest::UnreserveImageSpace() {
291 image_reservation_.reset();
292}
293
294} // namespace art