blob: 63abfeb71e3db66e7e4ae7cef1643feeac5acfb2 [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"
21#include "art_method.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070022#include "base/enums.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "class_linker.h"
24#include "compiled_method.h"
25#include "dex/quick_compiler_callbacks.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "dex/quick/dex_file_to_method_inliner_map.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_loader.h"
32#include "mirror/class-inl.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"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "thread-inl.h"
38#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 }
56 if (compiled_method != nullptr) {
Vladimir Marko35831e82015-09-11 11:59:18 +010057 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
58 uint32_t code_size = code.size();
Elliott Hughes956af0f2014-12-11 14:34:28 -080059 CHECK_NE(0u, code_size);
Vladimir Marko35831e82015-09-11 11:59:18 +010060 ArrayRef<const uint8_t> vmap_table = compiled_method->GetVmapTable();
61 uint32_t vmap_table_offset = vmap_table.empty() ? 0u
62 : sizeof(OatQuickMethodHeader) + vmap_table.size();
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010063 OatQuickMethodHeader method_header(vmap_table_offset,
Elliott Hughes956af0f2014-12-11 14:34:28 -080064 compiled_method->GetFrameSizeInBytes(),
65 compiled_method->GetCoreSpillMask(),
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010066 compiled_method->GetFpSpillMask(),
67 code_size);
Ian Rogerse63db272014-07-15 15:36:11 -070068
Elliott Hughes956af0f2014-12-11 14:34:28 -080069 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
70 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
Vladimir Marko562ff442015-10-27 18:51:20 +000071 const size_t max_padding = GetInstructionSetAlignment(compiled_method->GetInstructionSet());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010072 const size_t size = vmap_table.size() + sizeof(method_header) + code_size;
Vladimir Marko562ff442015-10-27 18:51:20 +000073 chunk->reserve(size + max_padding);
Elliott Hughes956af0f2014-12-11 14:34:28 -080074 chunk->resize(sizeof(method_header));
75 memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
Vladimir Marko35831e82015-09-11 11:59:18 +010076 chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end());
Vladimir Marko35831e82015-09-11 11:59:18 +010077 chunk->insert(chunk->end(), code.begin(), code.end());
Vladimir Marko562ff442015-10-27 18:51:20 +000078 CHECK_EQ(chunk->size(), size);
79 const void* unaligned_code_ptr = chunk->data() + (size - code_size);
80 size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr));
81 size_t padding = compiled_method->AlignCode(offset) - offset;
82 // Make sure no resizing takes place.
83 CHECK_GE(chunk->capacity(), chunk->size() + padding);
84 chunk->insert(chunk->begin(), padding, 0);
85 const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding;
86 CHECK_EQ(code_ptr, static_cast<const void*>(chunk->data() + (chunk->size() - code_size)));
Vladimir Marko35831e82015-09-11 11:59:18 +010087 MakeExecutable(code_ptr, code.size());
Ian Rogerse63db272014-07-15 15:36:11 -070088 const void* method_code = CompiledMethod::CodePointer(code_ptr,
89 compiled_method->GetInstructionSet());
90 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
Elliott Hughes956af0f2014-12-11 14:34:28 -080091 class_linker_->SetEntryPointsToCompiledCode(method, method_code);
Ian Rogerse63db272014-07-15 15:36:11 -070092 } else {
93 // No code? You must mean to go into the interpreter.
94 // Or the generic JNI...
Ian Rogers6f3dbba2014-10-14 17:41:57 -070095 class_linker_->SetEntryPointsToInterpreter(method);
Ian Rogerse63db272014-07-15 15:36:11 -070096 }
97}
98
99void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
100 CHECK(code_start != nullptr);
101 CHECK_NE(code_length, 0U);
102 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
103 uintptr_t base = RoundDown(data, kPageSize);
104 uintptr_t limit = RoundUp(data + code_length, kPageSize);
105 uintptr_t len = limit - base;
106 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
107 CHECK_EQ(result, 0);
108
Roland Levillain32430262016-02-01 15:23:20 +0000109 FlushInstructionCache(reinterpret_cast<char*>(base), reinterpret_cast<char*>(base + len));
Ian Rogerse63db272014-07-15 15:36:11 -0700110}
111
Mathieu Chartier0795f232016-09-27 18:43:30 -0700112void CommonCompilerTest::MakeExecutable(ObjPtr<mirror::ClassLoader> class_loader,
113 const char* class_name) {
Ian Rogerse63db272014-07-15 15:36:11 -0700114 std::string class_descriptor(DotToDescriptor(class_name));
115 Thread* self = Thread::Current();
116 StackHandleScope<1> hs(self);
117 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
118 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
119 CHECK(klass != nullptr) << "Class not found " << class_name;
Andreas Gampe542451c2016-07-26 09:02:02 -0700120 PointerSize pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800121 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700122 MakeExecutable(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700123 }
124}
125
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700126// Get the set of image classes given to the compiler-driver in SetUp. Note: the compiler
127// driver assumes ownership of the set, so the test should properly release the set.
128std::unordered_set<std::string>* CommonCompilerTest::GetImageClasses() {
129 // Empty set: by default no classes are retained in the image.
130 return new std::unordered_set<std::string>();
131}
132
133// Get the set of compiled 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::GetCompiledClasses() {
136 // Null, no selection of compiled-classes.
137 return nullptr;
138}
139
140// Get the set of compiled methods 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::GetCompiledMethods() {
143 // Null, no selection of compiled-methods.
144 return nullptr;
145}
146
Calin Juravle877fd962016-01-05 14:29:29 +0000147// Get ProfileCompilationInfo that should be passed to the driver.
148ProfileCompilationInfo* CommonCompilerTest::GetProfileCompilationInfo() {
149 // Null, profile information will not be taken into account.
150 return nullptr;
151}
152
Ian Rogerse63db272014-07-15 15:36:11 -0700153void CommonCompilerTest::SetUp() {
154 CommonRuntimeTest::SetUp();
155 {
156 ScopedObjectAccess soa(Thread::Current());
157
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700158 const InstructionSet instruction_set = kRuntimeISA;
Ian Rogerse63db272014-07-15 15:36:11 -0700159 // Take the default set of instruction features from the build.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700160 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
Ian Rogerse63db272014-07-15 15:36:11 -0700161
162 runtime_->SetInstructionSet(instruction_set);
163 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
164 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
165 if (!runtime_->HasCalleeSaveMethod(type)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700166 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
Ian Rogerse63db272014-07-15 15:36:11 -0700167 }
168 }
169
Ian Rogerse63db272014-07-15 15:36:11 -0700170 timer_.reset(new CumulativeLogger("Compilation times"));
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800171 CreateCompilerDriver(compiler_kind_, instruction_set);
Ian Rogerse63db272014-07-15 15:36:11 -0700172 }
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800173}
174
Andreas Gampe3f41a012016-02-18 16:53:41 -0800175void CommonCompilerTest::CreateCompilerDriver(Compiler::Kind kind,
176 InstructionSet isa,
177 size_t number_of_threads) {
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800178 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
179 verification_results_.get(),
180 method_inliner_map_.get(),
181 kind,
182 isa,
183 instruction_set_features_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000184 /* boot_image */ true,
Mathieu Chartier91288d82016-04-28 09:44:54 -0700185 /* app_image */ false,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800186 GetImageClasses(),
187 GetCompiledClasses(),
188 GetCompiledMethods(),
Andreas Gampe3f41a012016-02-18 16:53:41 -0800189 number_of_threads,
Vladimir Marko944da602016-02-19 12:27:55 +0000190 /* dump_stats */ true,
191 /* dump_passes */ true,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800192 timer_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000193 /* swap_fd */ -1,
Calin Juravle877fd962016-01-05 14:29:29 +0000194 GetProfileCompilationInfo()));
Ian Rogerse63db272014-07-15 15:36:11 -0700195 // We typically don't generate an image in unit tests, disable this optimization by default.
196 compiler_driver_->SetSupportBootImageFixup(false);
197}
198
199void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
200 CommonRuntimeTest::SetUpRuntimeOptions(options);
201
202 compiler_options_.reset(new CompilerOptions);
203 verification_results_.reset(new VerificationResults(compiler_options_.get()));
204 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
205 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700206 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700207 CompilerCallbacks::CallbackMode::kCompileApp));
Ian Rogerse63db272014-07-15 15:36:11 -0700208}
209
Roland Levillainbbcc01a2015-06-30 14:16:48 +0100210Compiler::Kind CommonCompilerTest::GetCompilerKind() const {
211 return compiler_kind_;
212}
213
214void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) {
215 compiler_kind_ = compiler_kind;
216}
217
Roland Levillain0d5a2812015-11-13 10:07:31 +0000218InstructionSet CommonCompilerTest::GetInstructionSet() const {
219 DCHECK(compiler_driver_.get() != nullptr);
220 return compiler_driver_->GetInstructionSet();
221}
222
Ian Rogerse63db272014-07-15 15:36:11 -0700223void CommonCompilerTest::TearDown() {
224 timer_.reset();
225 compiler_driver_.reset();
226 callbacks_.reset();
227 method_inliner_map_.reset();
228 verification_results_.reset();
229 compiler_options_.reset();
Mathieu Chartier496577f2016-09-20 15:33:31 -0700230 image_reservation_.reset();
Ian Rogerse63db272014-07-15 15:36:11 -0700231
232 CommonRuntimeTest::TearDown();
233}
234
235void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
236 std::string class_descriptor(DotToDescriptor(class_name));
237 Thread* self = Thread::Current();
238 StackHandleScope<1> hs(self);
239 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
240 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
241 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700242 auto pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800243 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700244 CompileMethod(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700245 }
246}
247
Mathieu Chartiere401d142015-04-22 13:56:20 -0700248void CommonCompilerTest::CompileMethod(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -0700249 CHECK(method != nullptr);
250 TimingLogger timings("CommonTest::CompileMethod", false, false);
251 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800252 compiler_driver_->CompileOne(Thread::Current(), method, &timings);
Ian Rogerse63db272014-07-15 15:36:11 -0700253 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
254 MakeExecutable(method);
255}
256
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700257void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
Ian Rogerse63db272014-07-15 15:36:11 -0700258 const char* class_name, const char* method_name,
259 const char* signature) {
260 std::string class_descriptor(DotToDescriptor(class_name));
261 Thread* self = Thread::Current();
262 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
263 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700264 auto pointer_size = class_linker_->GetImagePointerSize();
265 ArtMethod* method = klass->FindDirectMethod(method_name, signature, pointer_size);
Ian Rogerse63db272014-07-15 15:36:11 -0700266 CHECK(method != nullptr) << "Direct method not found: "
267 << class_name << "." << method_name << signature;
268 CompileMethod(method);
269}
270
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700271void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700272 const char* class_name, const char* method_name,
273 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700274 std::string class_descriptor(DotToDescriptor(class_name));
275 Thread* self = Thread::Current();
276 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
277 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 auto pointer_size = class_linker_->GetImagePointerSize();
279 ArtMethod* method = klass->FindVirtualMethod(method_name, signature, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700280 CHECK(method != nullptr) << "Virtual method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700281 << class_name << "." << method_name << signature;
282 CompileMethod(method);
283}
284
285void CommonCompilerTest::ReserveImageSpace() {
286 // Reserve where the image will be loaded up front so that other parts of test set up don't
287 // accidentally end up colliding with the fixed memory address when we need to load the image.
288 std::string error_msg;
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700289 MemMap::Init();
Ian Rogerse63db272014-07-15 15:36:11 -0700290 image_reservation_.reset(MemMap::MapAnonymous("image reservation",
Ian Rogers13735952014-10-08 12:43:28 -0700291 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
Hiroshi Yamauchi1d6fdaf2016-04-07 11:31:26 -0700292 (size_t)120 * 1024 * 1024, // 120MB
Ian Rogerse63db272014-07-15 15:36:11 -0700293 PROT_NONE,
294 false /* no need for 4gb flag with fixed mmap*/,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000295 false /* not reusing existing reservation */,
Ian Rogerse63db272014-07-15 15:36:11 -0700296 &error_msg));
297 CHECK(image_reservation_.get() != nullptr) << error_msg;
298}
299
300void CommonCompilerTest::UnreserveImageSpace() {
301 image_reservation_.reset();
302}
303
304} // namespace art