blob: 500fc4ae9a112257c4c21143fc32e217922ffd10 [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
Ian Rogersd582fa42014-11-05 23:46:43 -080019#include "arch/instruction_set_features.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_field-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080021#include "art_method-inl.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070022#include "base/callee_save_type.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070023#include "base/enums.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "class_linker.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010025#include "compiled_method-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "dex/quick_compiler_callbacks.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080027#include "dex/verification_results.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000029#include "driver/compiler_options.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "interpreter/interpreter.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "mirror/class_loader.h"
Ian Rogerse63db272014-07-15 15:36:11 -070033#include "mirror/dex_cache.h"
34#include "mirror/object-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010035#include "oat_quick_method_header.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070036#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070037#include "thread-current-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070038#include "utils.h"
39
40namespace art {
41
Ian Rogerse63db272014-07-15 15:36:11 -070042CommonCompilerTest::CommonCompilerTest() {}
43CommonCompilerTest::~CommonCompilerTest() {}
44
Mathieu Chartiere401d142015-04-22 13:56:20 -070045void CommonCompilerTest::MakeExecutable(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -070046 CHECK(method != nullptr);
47
48 const CompiledMethod* compiled_method = nullptr;
49 if (!method->IsAbstract()) {
50 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
51 const DexFile& dex_file = *dex_cache->GetDexFile();
52 compiled_method =
53 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
54 method->GetDexMethodIndex()));
55 }
Calin Juravlee0ac1152017-02-13 19:03:47 -080056 // If the code size is 0 it means the method was skipped due to profile guided compilation.
57 if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0u) {
Vladimir Marko35831e82015-09-11 11:59:18 +010058 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070059 const uint32_t code_size = code.size();
Vladimir Marko35831e82015-09-11 11:59:18 +010060 ArrayRef<const uint8_t> vmap_table = compiled_method->GetVmapTable();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070061 const uint32_t vmap_table_offset = vmap_table.empty() ? 0u
Vladimir Marko35831e82015-09-11 11:59:18 +010062 : sizeof(OatQuickMethodHeader) + vmap_table.size();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070063 // The method info is directly before the vmap table.
64 ArrayRef<const uint8_t> method_info = compiled_method->GetMethodInfo();
65 const uint32_t method_info_offset = method_info.empty() ? 0u
66 : vmap_table_offset + method_info.size();
67
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010068 OatQuickMethodHeader method_header(vmap_table_offset,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070069 method_info_offset,
Elliott Hughes956af0f2014-12-11 14:34:28 -080070 compiled_method->GetFrameSizeInBytes(),
71 compiled_method->GetCoreSpillMask(),
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010072 compiled_method->GetFpSpillMask(),
73 code_size);
Ian Rogerse63db272014-07-15 15:36:11 -070074
Elliott Hughes956af0f2014-12-11 14:34:28 -080075 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
76 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
Vladimir Marko562ff442015-10-27 18:51:20 +000077 const size_t max_padding = GetInstructionSetAlignment(compiled_method->GetInstructionSet());
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070078 const size_t size = method_info.size() + vmap_table.size() + sizeof(method_header) + code_size;
Vladimir Marko562ff442015-10-27 18:51:20 +000079 chunk->reserve(size + max_padding);
Elliott Hughes956af0f2014-12-11 14:34:28 -080080 chunk->resize(sizeof(method_header));
81 memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
Vladimir Marko35831e82015-09-11 11:59:18 +010082 chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end());
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070083 chunk->insert(chunk->begin(), method_info.begin(), method_info.end());
Vladimir Marko35831e82015-09-11 11:59:18 +010084 chunk->insert(chunk->end(), code.begin(), code.end());
Vladimir Marko562ff442015-10-27 18:51:20 +000085 CHECK_EQ(chunk->size(), size);
86 const void* unaligned_code_ptr = chunk->data() + (size - code_size);
87 size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr));
88 size_t padding = compiled_method->AlignCode(offset) - offset;
89 // Make sure no resizing takes place.
90 CHECK_GE(chunk->capacity(), chunk->size() + padding);
91 chunk->insert(chunk->begin(), padding, 0);
92 const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding;
93 CHECK_EQ(code_ptr, static_cast<const void*>(chunk->data() + (chunk->size() - code_size)));
Vladimir Marko35831e82015-09-11 11:59:18 +010094 MakeExecutable(code_ptr, code.size());
Ian Rogerse63db272014-07-15 15:36:11 -070095 const void* method_code = CompiledMethod::CodePointer(code_ptr,
96 compiled_method->GetInstructionSet());
David Sehr709b0702016-10-13 09:12:37 -070097 LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code;
Vladimir Markofbfc3942017-07-27 16:51:35 +010098 method->SetEntryPointFromQuickCompiledCode(method_code);
Ian Rogerse63db272014-07-15 15:36:11 -070099 } else {
100 // No code? You must mean to go into the interpreter.
101 // Or the generic JNI...
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700102 class_linker_->SetEntryPointsToInterpreter(method);
Ian Rogerse63db272014-07-15 15:36:11 -0700103 }
104}
105
106void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
107 CHECK(code_start != nullptr);
108 CHECK_NE(code_length, 0U);
109 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
110 uintptr_t base = RoundDown(data, kPageSize);
111 uintptr_t limit = RoundUp(data + code_length, kPageSize);
112 uintptr_t len = limit - base;
113 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
114 CHECK_EQ(result, 0);
115
Roland Levillain32430262016-02-01 15:23:20 +0000116 FlushInstructionCache(reinterpret_cast<char*>(base), reinterpret_cast<char*>(base + len));
Ian Rogerse63db272014-07-15 15:36:11 -0700117}
118
Mathieu Chartier0795f232016-09-27 18:43:30 -0700119void CommonCompilerTest::MakeExecutable(ObjPtr<mirror::ClassLoader> class_loader,
120 const char* class_name) {
Ian Rogerse63db272014-07-15 15:36:11 -0700121 std::string class_descriptor(DotToDescriptor(class_name));
122 Thread* self = Thread::Current();
123 StackHandleScope<1> hs(self);
124 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
125 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
126 CHECK(klass != nullptr) << "Class not found " << class_name;
Andreas Gampe542451c2016-07-26 09:02:02 -0700127 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800128 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129 MakeExecutable(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700130 }
131}
132
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700133// Get the set of image classes given to the compiler-driver in SetUp. Note: the compiler
134// driver assumes ownership of the set, so the test should properly release the set.
135std::unordered_set<std::string>* CommonCompilerTest::GetImageClasses() {
136 // Empty set: by default no classes are retained in the image.
137 return new std::unordered_set<std::string>();
138}
139
140// Get the set of compiled classes given to the compiler-driver in SetUp. Note: the compiler
141// driver assumes ownership of the set, so the test should properly release the set.
142std::unordered_set<std::string>* CommonCompilerTest::GetCompiledClasses() {
143 // Null, no selection of compiled-classes.
144 return nullptr;
145}
146
147// Get the set of compiled methods given to the compiler-driver in SetUp. Note: the compiler
148// driver assumes ownership of the set, so the test should properly release the set.
149std::unordered_set<std::string>* CommonCompilerTest::GetCompiledMethods() {
150 // Null, no selection of compiled-methods.
151 return nullptr;
152}
153
Calin Juravle877fd962016-01-05 14:29:29 +0000154// Get ProfileCompilationInfo that should be passed to the driver.
155ProfileCompilationInfo* CommonCompilerTest::GetProfileCompilationInfo() {
156 // Null, profile information will not be taken into account.
157 return nullptr;
158}
159
Ian Rogerse63db272014-07-15 15:36:11 -0700160void CommonCompilerTest::SetUp() {
161 CommonRuntimeTest::SetUp();
162 {
163 ScopedObjectAccess soa(Thread::Current());
164
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700165 const InstructionSet instruction_set = kRuntimeISA;
Ian Rogerse63db272014-07-15 15:36:11 -0700166 // Take the default set of instruction features from the build.
Andreas Gampe0415b4e2015-01-06 15:17:07 -0800167 instruction_set_features_ = InstructionSetFeatures::FromCppDefines();
Ian Rogerse63db272014-07-15 15:36:11 -0700168
169 runtime_->SetInstructionSet(instruction_set);
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700170 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); ++i) {
171 CalleeSaveType type = CalleeSaveType(i);
Ian Rogerse63db272014-07-15 15:36:11 -0700172 if (!runtime_->HasCalleeSaveMethod(type)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700173 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
Ian Rogerse63db272014-07-15 15:36:11 -0700174 }
175 }
176
Ian Rogerse63db272014-07-15 15:36:11 -0700177 timer_.reset(new CumulativeLogger("Compilation times"));
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800178 CreateCompilerDriver(compiler_kind_, instruction_set);
Ian Rogerse63db272014-07-15 15:36:11 -0700179 }
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800180}
181
Andreas Gampe3f41a012016-02-18 16:53:41 -0800182void CommonCompilerTest::CreateCompilerDriver(Compiler::Kind kind,
183 InstructionSet isa,
184 size_t number_of_threads) {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000185 compiler_options_->boot_image_ = true;
Mathieu Chartierd0af56c2017-02-17 12:56:25 -0800186 compiler_options_->SetCompilerFilter(GetCompilerFilter());
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800187 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
188 verification_results_.get(),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800189 kind,
190 isa,
191 instruction_set_features_.get(),
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800192 GetImageClasses(),
193 GetCompiledClasses(),
194 GetCompiledMethods(),
Andreas Gampe3f41a012016-02-18 16:53:41 -0800195 number_of_threads,
Vladimir Marko944da602016-02-19 12:27:55 +0000196 /* dump_stats */ true,
197 /* dump_passes */ true,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800198 timer_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000199 /* swap_fd */ -1,
Calin Juravle877fd962016-01-05 14:29:29 +0000200 GetProfileCompilationInfo()));
Ian Rogerse63db272014-07-15 15:36:11 -0700201 // We typically don't generate an image in unit tests, disable this optimization by default.
202 compiler_driver_->SetSupportBootImageFixup(false);
203}
204
205void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
206 CommonRuntimeTest::SetUpRuntimeOptions(options);
207
208 compiler_options_.reset(new CompilerOptions);
209 verification_results_.reset(new VerificationResults(compiler_options_.get()));
Mathieu Chartiere01b6f62017-07-19 16:55:04 -0700210 QuickCompilerCallbacks* callbacks =
211 new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp);
212 callbacks->SetVerificationResults(verification_results_.get());
213 callbacks_.reset(callbacks);
Ian Rogerse63db272014-07-15 15:36:11 -0700214}
215
Roland Levillainbbcc01a2015-06-30 14:16:48 +0100216Compiler::Kind CommonCompilerTest::GetCompilerKind() const {
217 return compiler_kind_;
218}
219
220void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) {
221 compiler_kind_ = compiler_kind;
222}
223
Roland Levillain0d5a2812015-11-13 10:07:31 +0000224InstructionSet CommonCompilerTest::GetInstructionSet() const {
225 DCHECK(compiler_driver_.get() != nullptr);
226 return compiler_driver_->GetInstructionSet();
227}
228
Ian Rogerse63db272014-07-15 15:36:11 -0700229void CommonCompilerTest::TearDown() {
230 timer_.reset();
231 compiler_driver_.reset();
232 callbacks_.reset();
Ian Rogerse63db272014-07-15 15:36:11 -0700233 verification_results_.reset();
234 compiler_options_.reset();
Mathieu Chartier496577f2016-09-20 15:33:31 -0700235 image_reservation_.reset();
Ian Rogerse63db272014-07-15 15:36:11 -0700236
237 CommonRuntimeTest::TearDown();
238}
239
240void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
241 std::string class_descriptor(DotToDescriptor(class_name));
242 Thread* self = Thread::Current();
243 StackHandleScope<1> hs(self);
244 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
245 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
246 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700247 auto pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800248 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 CompileMethod(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700250 }
251}
252
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253void CommonCompilerTest::CompileMethod(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -0700254 CHECK(method != nullptr);
255 TimingLogger timings("CommonTest::CompileMethod", false, false);
256 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800257 compiler_driver_->CompileOne(Thread::Current(), method, &timings);
Ian Rogerse63db272014-07-15 15:36:11 -0700258 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
259 MakeExecutable(method);
260}
261
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700262void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
Ian Rogerse63db272014-07-15 15:36:11 -0700263 const char* class_name, const char* method_name,
264 const char* signature) {
265 std::string class_descriptor(DotToDescriptor(class_name));
266 Thread* self = Thread::Current();
267 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
268 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 auto pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100270 ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size);
271 CHECK(method != nullptr && method->IsDirect()) << "Direct method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700272 << class_name << "." << method_name << signature;
273 CompileMethod(method);
274}
275
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700276void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700277 const char* class_name, const char* method_name,
278 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700279 std::string class_descriptor(DotToDescriptor(class_name));
280 Thread* self = Thread::Current();
281 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
282 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700283 auto pointer_size = class_linker_->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100284 ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size);
285 CHECK(method != nullptr && !method->IsDirect()) << "Virtual method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700286 << class_name << "." << method_name << signature;
287 CompileMethod(method);
288}
289
290void CommonCompilerTest::ReserveImageSpace() {
291 // Reserve where the image will be loaded up front so that other parts of test set up don't
292 // accidentally end up colliding with the fixed memory address when we need to load the image.
293 std::string error_msg;
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700294 MemMap::Init();
Ian Rogerse63db272014-07-15 15:36:11 -0700295 image_reservation_.reset(MemMap::MapAnonymous("image reservation",
Ian Rogers13735952014-10-08 12:43:28 -0700296 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
Hiroshi Yamauchi1d6fdaf2016-04-07 11:31:26 -0700297 (size_t)120 * 1024 * 1024, // 120MB
Ian Rogerse63db272014-07-15 15:36:11 -0700298 PROT_NONE,
299 false /* no need for 4gb flag with fixed mmap*/,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000300 false /* not reusing existing reservation */,
Ian Rogerse63db272014-07-15 15:36:11 -0700301 &error_msg));
302 CHECK(image_reservation_.get() != nullptr) << error_msg;
303}
304
305void CommonCompilerTest::UnreserveImageSpace() {
306 image_reservation_.reset();
307}
308
309} // namespace art