blob: 0001b672be001851b1d356dc10afcc2c854f1429 [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"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "class_linker.h"
23#include "compiled_method.h"
24#include "dex/quick_compiler_callbacks.h"
Ian Rogerse63db272014-07-15 15:36:11 -070025#include "dex/quick/dex_file_to_method_inliner_map.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080026#include "dex/verification_results.h"
Ian Rogerse63db272014-07-15 15:36:11 -070027#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000028#include "driver/compiler_options.h"
Ian Rogerse63db272014-07-15 15:36:11 -070029#include "interpreter/interpreter.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "mirror/class_loader.h"
31#include "mirror/class-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070032#include "mirror/dex_cache.h"
33#include "mirror/object-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010034#include "oat_quick_method_header.h"
Ian Rogerse63db272014-07-15 15:36:11 -070035#include "scoped_thread_state_change.h"
36#include "thread-inl.h"
37#include "utils.h"
38
39namespace art {
40
Ian Rogerse63db272014-07-15 15:36:11 -070041CommonCompilerTest::CommonCompilerTest() {}
42CommonCompilerTest::~CommonCompilerTest() {}
43
Mathieu Chartiere401d142015-04-22 13:56:20 -070044void CommonCompilerTest::MakeExecutable(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -070045 CHECK(method != nullptr);
46
47 const CompiledMethod* compiled_method = nullptr;
48 if (!method->IsAbstract()) {
49 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
50 const DexFile& dex_file = *dex_cache->GetDexFile();
51 compiled_method =
52 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
53 method->GetDexMethodIndex()));
54 }
55 if (compiled_method != nullptr) {
Vladimir Marko35831e82015-09-11 11:59:18 +010056 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
57 uint32_t code_size = code.size();
Elliott Hughes956af0f2014-12-11 14:34:28 -080058 CHECK_NE(0u, code_size);
Vladimir Marko35831e82015-09-11 11:59:18 +010059 ArrayRef<const uint8_t> vmap_table = compiled_method->GetVmapTable();
60 uint32_t vmap_table_offset = vmap_table.empty() ? 0u
61 : sizeof(OatQuickMethodHeader) + vmap_table.size();
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010062 OatQuickMethodHeader method_header(vmap_table_offset,
Elliott Hughes956af0f2014-12-11 14:34:28 -080063 compiled_method->GetFrameSizeInBytes(),
64 compiled_method->GetCoreSpillMask(),
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010065 compiled_method->GetFpSpillMask(),
66 code_size);
Ian Rogerse63db272014-07-15 15:36:11 -070067
Elliott Hughes956af0f2014-12-11 14:34:28 -080068 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
69 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
Vladimir Marko562ff442015-10-27 18:51:20 +000070 const size_t max_padding = GetInstructionSetAlignment(compiled_method->GetInstructionSet());
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010071 const size_t size = vmap_table.size() + sizeof(method_header) + code_size;
Vladimir Marko562ff442015-10-27 18:51:20 +000072 chunk->reserve(size + max_padding);
Elliott Hughes956af0f2014-12-11 14:34:28 -080073 chunk->resize(sizeof(method_header));
74 memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
Vladimir Marko35831e82015-09-11 11:59:18 +010075 chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end());
Vladimir Marko35831e82015-09-11 11:59:18 +010076 chunk->insert(chunk->end(), code.begin(), code.end());
Vladimir Marko562ff442015-10-27 18:51:20 +000077 CHECK_EQ(chunk->size(), size);
78 const void* unaligned_code_ptr = chunk->data() + (size - code_size);
79 size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr));
80 size_t padding = compiled_method->AlignCode(offset) - offset;
81 // Make sure no resizing takes place.
82 CHECK_GE(chunk->capacity(), chunk->size() + padding);
83 chunk->insert(chunk->begin(), padding, 0);
84 const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding;
85 CHECK_EQ(code_ptr, static_cast<const void*>(chunk->data() + (chunk->size() - code_size)));
Vladimir Marko35831e82015-09-11 11:59:18 +010086 MakeExecutable(code_ptr, code.size());
Ian Rogerse63db272014-07-15 15:36:11 -070087 const void* method_code = CompiledMethod::CodePointer(code_ptr,
88 compiled_method->GetInstructionSet());
89 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
Elliott Hughes956af0f2014-12-11 14:34:28 -080090 class_linker_->SetEntryPointsToCompiledCode(method, method_code);
Ian Rogerse63db272014-07-15 15:36:11 -070091 } else {
92 // No code? You must mean to go into the interpreter.
93 // Or the generic JNI...
Ian Rogers6f3dbba2014-10-14 17:41:57 -070094 class_linker_->SetEntryPointsToInterpreter(method);
Ian Rogerse63db272014-07-15 15:36:11 -070095 }
96}
97
98void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
99 CHECK(code_start != nullptr);
100 CHECK_NE(code_length, 0U);
101 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
102 uintptr_t base = RoundDown(data, kPageSize);
103 uintptr_t limit = RoundUp(data + code_length, kPageSize);
104 uintptr_t len = limit - base;
105 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
106 CHECK_EQ(result, 0);
107
Roland Levillain32430262016-02-01 15:23:20 +0000108 FlushInstructionCache(reinterpret_cast<char*>(base), reinterpret_cast<char*>(base + len));
Ian Rogerse63db272014-07-15 15:36:11 -0700109}
110
111void CommonCompilerTest::MakeExecutable(mirror::ClassLoader* class_loader, const char* class_name) {
112 std::string class_descriptor(DotToDescriptor(class_name));
113 Thread* self = Thread::Current();
114 StackHandleScope<1> hs(self);
115 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
116 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
117 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 size_t pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800119 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700120 MakeExecutable(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700121 }
122}
123
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700124// Get the set of image classes given to the compiler-driver in SetUp. Note: the compiler
125// driver assumes ownership of the set, so the test should properly release the set.
126std::unordered_set<std::string>* CommonCompilerTest::GetImageClasses() {
127 // Empty set: by default no classes are retained in the image.
128 return new std::unordered_set<std::string>();
129}
130
131// Get the set of compiled classes given to the compiler-driver in SetUp. Note: the compiler
132// driver assumes ownership of the set, so the test should properly release the set.
133std::unordered_set<std::string>* CommonCompilerTest::GetCompiledClasses() {
134 // Null, no selection of compiled-classes.
135 return nullptr;
136}
137
138// Get the set of compiled methods given to the compiler-driver in SetUp. Note: the compiler
139// driver assumes ownership of the set, so the test should properly release the set.
140std::unordered_set<std::string>* CommonCompilerTest::GetCompiledMethods() {
141 // Null, no selection of compiled-methods.
142 return nullptr;
143}
144
Calin Juravle877fd962016-01-05 14:29:29 +0000145// Get ProfileCompilationInfo that should be passed to the driver.
146ProfileCompilationInfo* CommonCompilerTest::GetProfileCompilationInfo() {
147 // Null, profile information will not be taken into account.
148 return nullptr;
149}
150
Ian Rogerse63db272014-07-15 15:36:11 -0700151void CommonCompilerTest::SetUp() {
152 CommonRuntimeTest::SetUp();
153 {
154 ScopedObjectAccess soa(Thread::Current());
155
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700156 const InstructionSet instruction_set = kRuntimeISA;
Ian Rogerse63db272014-07-15 15:36:11 -0700157 // Take the default set of instruction features from the build.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700158 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
Ian Rogerse63db272014-07-15 15:36:11 -0700159
160 runtime_->SetInstructionSet(instruction_set);
161 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
162 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
163 if (!runtime_->HasCalleeSaveMethod(type)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700164 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
Ian Rogerse63db272014-07-15 15:36:11 -0700165 }
166 }
167
Ian Rogerse63db272014-07-15 15:36:11 -0700168 timer_.reset(new CumulativeLogger("Compilation times"));
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800169 CreateCompilerDriver(compiler_kind_, instruction_set);
Ian Rogerse63db272014-07-15 15:36:11 -0700170 }
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800171}
172
Andreas Gampe3f41a012016-02-18 16:53:41 -0800173void CommonCompilerTest::CreateCompilerDriver(Compiler::Kind kind,
174 InstructionSet isa,
175 size_t number_of_threads) {
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800176 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
177 verification_results_.get(),
178 method_inliner_map_.get(),
179 kind,
180 isa,
181 instruction_set_features_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000182 /* boot_image */ true,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800183 GetImageClasses(),
184 GetCompiledClasses(),
185 GetCompiledMethods(),
Andreas Gampe3f41a012016-02-18 16:53:41 -0800186 number_of_threads,
Vladimir Marko944da602016-02-19 12:27:55 +0000187 /* dump_stats */ true,
188 /* dump_passes */ true,
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800189 timer_.get(),
Vladimir Marko944da602016-02-19 12:27:55 +0000190 /* swap_fd */ -1,
Calin Juravle877fd962016-01-05 14:29:29 +0000191 GetProfileCompilationInfo()));
Ian Rogerse63db272014-07-15 15:36:11 -0700192 // We typically don't generate an image in unit tests, disable this optimization by default.
193 compiler_driver_->SetSupportBootImageFixup(false);
194}
195
196void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
197 CommonRuntimeTest::SetUpRuntimeOptions(options);
198
199 compiler_options_.reset(new CompilerOptions);
200 verification_results_.reset(new VerificationResults(compiler_options_.get()));
201 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
202 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700203 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700204 CompilerCallbacks::CallbackMode::kCompileApp));
Ian Rogerse63db272014-07-15 15:36:11 -0700205}
206
Roland Levillainbbcc01a2015-06-30 14:16:48 +0100207Compiler::Kind CommonCompilerTest::GetCompilerKind() const {
208 return compiler_kind_;
209}
210
211void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) {
212 compiler_kind_ = compiler_kind;
213}
214
Roland Levillain0d5a2812015-11-13 10:07:31 +0000215InstructionSet CommonCompilerTest::GetInstructionSet() const {
216 DCHECK(compiler_driver_.get() != nullptr);
217 return compiler_driver_->GetInstructionSet();
218}
219
Ian Rogerse63db272014-07-15 15:36:11 -0700220void CommonCompilerTest::TearDown() {
221 timer_.reset();
222 compiler_driver_.reset();
223 callbacks_.reset();
224 method_inliner_map_.reset();
225 verification_results_.reset();
226 compiler_options_.reset();
227
228 CommonRuntimeTest::TearDown();
229}
230
231void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
232 std::string class_descriptor(DotToDescriptor(class_name));
233 Thread* self = Thread::Current();
234 StackHandleScope<1> hs(self);
235 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
236 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
237 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 auto pointer_size = class_linker_->GetImagePointerSize();
Alex Lighte64300b2015-12-15 15:02:47 -0800239 for (auto& m : klass->GetMethods(pointer_size)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700240 CompileMethod(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700241 }
242}
243
Mathieu Chartiere401d142015-04-22 13:56:20 -0700244void CommonCompilerTest::CompileMethod(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -0700245 CHECK(method != nullptr);
246 TimingLogger timings("CommonTest::CompileMethod", false, false);
247 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800248 compiler_driver_->CompileOne(Thread::Current(), method, &timings);
Ian Rogerse63db272014-07-15 15:36:11 -0700249 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
250 MakeExecutable(method);
251}
252
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700253void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
Ian Rogerse63db272014-07-15 15:36:11 -0700254 const char* class_name, const char* method_name,
255 const char* signature) {
256 std::string class_descriptor(DotToDescriptor(class_name));
257 Thread* self = Thread::Current();
258 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
259 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700260 auto pointer_size = class_linker_->GetImagePointerSize();
261 ArtMethod* method = klass->FindDirectMethod(method_name, signature, pointer_size);
Ian Rogerse63db272014-07-15 15:36:11 -0700262 CHECK(method != nullptr) << "Direct method not found: "
263 << class_name << "." << method_name << signature;
264 CompileMethod(method);
265}
266
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700267void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700268 const char* class_name, const char* method_name,
269 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700270 std::string class_descriptor(DotToDescriptor(class_name));
271 Thread* self = Thread::Current();
272 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
273 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274 auto pointer_size = class_linker_->GetImagePointerSize();
275 ArtMethod* method = klass->FindVirtualMethod(method_name, signature, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700276 CHECK(method != nullptr) << "Virtual method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700277 << class_name << "." << method_name << signature;
278 CompileMethod(method);
279}
280
281void CommonCompilerTest::ReserveImageSpace() {
282 // Reserve where the image will be loaded up front so that other parts of test set up don't
283 // accidentally end up colliding with the fixed memory address when we need to load the image.
284 std::string error_msg;
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700285 MemMap::Init();
Ian Rogerse63db272014-07-15 15:36:11 -0700286 image_reservation_.reset(MemMap::MapAnonymous("image reservation",
Ian Rogers13735952014-10-08 12:43:28 -0700287 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
Ian Rogerse63db272014-07-15 15:36:11 -0700288 (size_t)100 * 1024 * 1024, // 100MB
289 PROT_NONE,
290 false /* no need for 4gb flag with fixed mmap*/,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000291 false /* not reusing existing reservation */,
Ian Rogerse63db272014-07-15 15:36:11 -0700292 &error_msg));
293 CHECK(image_reservation_.get() != nullptr) << error_msg;
294}
295
296void CommonCompilerTest::UnreserveImageSpace() {
297 image_reservation_.reset();
298}
299
300} // namespace art