blob: e6cc50cc5e9642c28baead103d6c860428e38bab [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"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080024#include "dex/pass_manager.h"
Ian Rogerse63db272014-07-15 15:36:11 -070025#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"
Ian Rogerse63db272014-07-15 15:36:11 -070036#include "scoped_thread_state_change.h"
37#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();
63 ArrayRef<const uint8_t> mapping_table = compiled_method->GetMappingTable();
64 bool mapping_table_used = !mapping_table.empty();
65 size_t mapping_table_size = mapping_table.size();
Andreas Gampea2d0afc2014-12-22 13:43:33 -080066 uint32_t mapping_table_offset = !mapping_table_used ? 0u
Vladimir Marko35831e82015-09-11 11:59:18 +010067 : sizeof(OatQuickMethodHeader) + vmap_table.size() + mapping_table_size;
68 ArrayRef<const uint8_t> gc_map = compiled_method->GetGcMap();
69 bool gc_map_used = !gc_map.empty();
70 size_t gc_map_size = gc_map.size();
Andreas Gampea2d0afc2014-12-22 13:43:33 -080071 uint32_t gc_map_offset = !gc_map_used ? 0u
Vladimir Marko35831e82015-09-11 11:59:18 +010072 : sizeof(OatQuickMethodHeader) + vmap_table.size() + mapping_table_size + gc_map_size;
Elliott Hughes956af0f2014-12-11 14:34:28 -080073 OatQuickMethodHeader method_header(mapping_table_offset, vmap_table_offset, gc_map_offset,
74 compiled_method->GetFrameSizeInBytes(),
75 compiled_method->GetCoreSpillMask(),
76 compiled_method->GetFpSpillMask(), 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());
81 const size_t size =
82 gc_map_size + mapping_table_size + vmap_table.size() + sizeof(method_header) + code_size;
83 chunk->reserve(size + max_padding);
Elliott Hughes956af0f2014-12-11 14:34:28 -080084 chunk->resize(sizeof(method_header));
85 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());
Andreas Gampea2d0afc2014-12-22 13:43:33 -080087 if (mapping_table_used) {
Vladimir Marko35831e82015-09-11 11:59:18 +010088 chunk->insert(chunk->begin(), mapping_table.begin(), mapping_table.end());
Andreas Gampea2d0afc2014-12-22 13:43:33 -080089 }
90 if (gc_map_used) {
Vladimir Marko35831e82015-09-11 11:59:18 +010091 chunk->insert(chunk->begin(), gc_map.begin(), gc_map.end());
Andreas Gampea2d0afc2014-12-22 13:43:33 -080092 }
Vladimir Marko35831e82015-09-11 11:59:18 +010093 chunk->insert(chunk->end(), code.begin(), code.end());
Vladimir Marko562ff442015-10-27 18:51:20 +000094 CHECK_EQ(chunk->size(), size);
95 const void* unaligned_code_ptr = chunk->data() + (size - code_size);
96 size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr));
97 size_t padding = compiled_method->AlignCode(offset) - offset;
98 // Make sure no resizing takes place.
99 CHECK_GE(chunk->capacity(), chunk->size() + padding);
100 chunk->insert(chunk->begin(), padding, 0);
101 const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding;
102 CHECK_EQ(code_ptr, static_cast<const void*>(chunk->data() + (chunk->size() - code_size)));
Vladimir Marko35831e82015-09-11 11:59:18 +0100103 MakeExecutable(code_ptr, code.size());
Ian Rogerse63db272014-07-15 15:36:11 -0700104 const void* method_code = CompiledMethod::CodePointer(code_ptr,
105 compiled_method->GetInstructionSet());
106 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800107 class_linker_->SetEntryPointsToCompiledCode(method, method_code);
Ian Rogerse63db272014-07-15 15:36:11 -0700108 } else {
109 // No code? You must mean to go into the interpreter.
110 // Or the generic JNI...
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700111 class_linker_->SetEntryPointsToInterpreter(method);
Ian Rogerse63db272014-07-15 15:36:11 -0700112 }
113}
114
115void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
116 CHECK(code_start != nullptr);
117 CHECK_NE(code_length, 0U);
118 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
119 uintptr_t base = RoundDown(data, kPageSize);
120 uintptr_t limit = RoundUp(data + code_length, kPageSize);
121 uintptr_t len = limit - base;
122 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
123 CHECK_EQ(result, 0);
124
125 // Flush instruction cache
126 // Only uses __builtin___clear_cache if GCC >= 4.3.3
127#if GCC_VERSION >= 40303
128 __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len));
129#else
130 // Only warn if not Intel as Intel doesn't have cache flush instructions.
131#if !defined(__i386__) && !defined(__x86_64__)
Ian Rogers2c4257b2014-10-24 14:20:06 -0700132 UNIMPLEMENTED(WARNING) << "cache flush";
Ian Rogerse63db272014-07-15 15:36:11 -0700133#endif
134#endif
135}
136
137void CommonCompilerTest::MakeExecutable(mirror::ClassLoader* class_loader, const char* class_name) {
138 std::string class_descriptor(DotToDescriptor(class_name));
139 Thread* self = Thread::Current();
140 StackHandleScope<1> hs(self);
141 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
142 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
143 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 size_t pointer_size = class_linker_->GetImagePointerSize();
145 for (auto& m : klass->GetDirectMethods(pointer_size)) {
146 MakeExecutable(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700147 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
149 MakeExecutable(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700150 }
151}
152
Andreas Gampe70bef0d2015-04-15 02:37:28 -0700153// Get the set of image classes given to the compiler-driver in SetUp. Note: the compiler
154// driver assumes ownership of the set, so the test should properly release the set.
155std::unordered_set<std::string>* CommonCompilerTest::GetImageClasses() {
156 // Empty set: by default no classes are retained in the image.
157 return new std::unordered_set<std::string>();
158}
159
160// Get the set of compiled classes given to the compiler-driver in SetUp. Note: the compiler
161// driver assumes ownership of the set, so the test should properly release the set.
162std::unordered_set<std::string>* CommonCompilerTest::GetCompiledClasses() {
163 // Null, no selection of compiled-classes.
164 return nullptr;
165}
166
167// Get the set of compiled methods given to the compiler-driver in SetUp. Note: the compiler
168// driver assumes ownership of the set, so the test should properly release the set.
169std::unordered_set<std::string>* CommonCompilerTest::GetCompiledMethods() {
170 // Null, no selection of compiled-methods.
171 return nullptr;
172}
173
Ian Rogerse63db272014-07-15 15:36:11 -0700174void CommonCompilerTest::SetUp() {
175 CommonRuntimeTest::SetUp();
176 {
177 ScopedObjectAccess soa(Thread::Current());
178
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700179 const InstructionSet instruction_set = kRuntimeISA;
Ian Rogerse63db272014-07-15 15:36:11 -0700180 // Take the default set of instruction features from the build.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700181 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
Ian Rogerse63db272014-07-15 15:36:11 -0700182
183 runtime_->SetInstructionSet(instruction_set);
184 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
185 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
186 if (!runtime_->HasCalleeSaveMethod(type)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700187 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
Ian Rogerse63db272014-07-15 15:36:11 -0700188 }
189 }
190
Ian Rogerse63db272014-07-15 15:36:11 -0700191 timer_.reset(new CumulativeLogger("Compilation times"));
Nicolas Geoffray83d4d722015-12-10 08:26:32 +0000192 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
193 verification_results_.get(),
194 method_inliner_map_.get(),
195 compiler_kind_, instruction_set,
196 instruction_set_features_.get(),
197 true,
198 GetImageClasses(),
199 GetCompiledClasses(),
200 GetCompiledMethods(),
201 2, true, true, "", false, timer_.get(), -1, ""));
Ian Rogerse63db272014-07-15 15:36:11 -0700202 }
203 // We typically don't generate an image in unit tests, disable this optimization by default.
204 compiler_driver_->SetSupportBootImageFixup(false);
205}
206
207void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
208 CommonRuntimeTest::SetUpRuntimeOptions(options);
209
210 compiler_options_.reset(new CompilerOptions);
211 verification_results_.reset(new VerificationResults(compiler_options_.get()));
212 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
213 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700214 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700215 CompilerCallbacks::CallbackMode::kCompileApp));
Ian Rogerse63db272014-07-15 15:36:11 -0700216}
217
Roland Levillainbbcc01a2015-06-30 14:16:48 +0100218Compiler::Kind CommonCompilerTest::GetCompilerKind() const {
219 return compiler_kind_;
220}
221
222void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) {
223 compiler_kind_ = compiler_kind;
224}
225
Roland Levillain0d5a2812015-11-13 10:07:31 +0000226InstructionSet CommonCompilerTest::GetInstructionSet() const {
227 DCHECK(compiler_driver_.get() != nullptr);
228 return compiler_driver_->GetInstructionSet();
229}
230
Ian Rogerse63db272014-07-15 15:36:11 -0700231void CommonCompilerTest::TearDown() {
232 timer_.reset();
233 compiler_driver_.reset();
234 callbacks_.reset();
235 method_inliner_map_.reset();
236 verification_results_.reset();
237 compiler_options_.reset();
238
239 CommonRuntimeTest::TearDown();
240}
241
242void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
243 std::string class_descriptor(DotToDescriptor(class_name));
244 Thread* self = Thread::Current();
245 StackHandleScope<1> hs(self);
246 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
247 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
248 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 auto pointer_size = class_linker_->GetImagePointerSize();
250 for (auto& m : klass->GetDirectMethods(pointer_size)) {
251 CompileMethod(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700252 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
254 CompileMethod(&m);
Ian Rogerse63db272014-07-15 15:36:11 -0700255 }
256}
257
Mathieu Chartiere401d142015-04-22 13:56:20 -0700258void CommonCompilerTest::CompileMethod(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -0700259 CHECK(method != nullptr);
260 TimingLogger timings("CommonTest::CompileMethod", false, false);
261 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800262 compiler_driver_->CompileOne(Thread::Current(), method, &timings);
Ian Rogerse63db272014-07-15 15:36:11 -0700263 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
264 MakeExecutable(method);
265}
266
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700267void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
Ian Rogerse63db272014-07-15 15:36:11 -0700268 const char* class_name, const char* method_name,
269 const char* signature) {
270 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->FindDirectMethod(method_name, signature, pointer_size);
Ian Rogerse63db272014-07-15 15:36:11 -0700276 CHECK(method != nullptr) << "Direct method not found: "
277 << class_name << "." << method_name << signature;
278 CompileMethod(method);
279}
280
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700281void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700282 const char* class_name, const char* method_name,
283 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700284 std::string class_descriptor(DotToDescriptor(class_name));
285 Thread* self = Thread::Current();
286 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
287 CHECK(klass != nullptr) << "Class not found " << class_name;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 auto pointer_size = class_linker_->GetImagePointerSize();
289 ArtMethod* method = klass->FindVirtualMethod(method_name, signature, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700290 CHECK(method != nullptr) << "Virtual method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700291 << class_name << "." << method_name << signature;
292 CompileMethod(method);
293}
294
295void CommonCompilerTest::ReserveImageSpace() {
296 // Reserve where the image will be loaded up front so that other parts of test set up don't
297 // accidentally end up colliding with the fixed memory address when we need to load the image.
298 std::string error_msg;
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700299 MemMap::Init();
Ian Rogerse63db272014-07-15 15:36:11 -0700300 image_reservation_.reset(MemMap::MapAnonymous("image reservation",
Ian Rogers13735952014-10-08 12:43:28 -0700301 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
Ian Rogerse63db272014-07-15 15:36:11 -0700302 (size_t)100 * 1024 * 1024, // 100MB
303 PROT_NONE,
304 false /* no need for 4gb flag with fixed mmap*/,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000305 false /* not reusing existing reservation */,
Ian Rogerse63db272014-07-15 15:36:11 -0700306 &error_msg));
307 CHECK(image_reservation_.get() != nullptr) << error_msg;
308}
309
310void CommonCompilerTest::UnreserveImageSpace() {
311 image_reservation_.reset();
312}
313
314} // namespace art