blob: e84f65a06e80376b7ae8bfa53fa7b558327cc430 [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"
Ian Rogerse63db272014-07-15 15:36:11 -070020#include "class_linker.h"
21#include "compiled_method.h"
22#include "dex/quick_compiler_callbacks.h"
23#include "dex/verification_results.h"
24#include "dex/quick/dex_file_to_method_inliner_map.h"
25#include "driver/compiler_driver.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "interpreter/interpreter.h"
27#include "mirror/art_method.h"
28#include "mirror/dex_cache.h"
29#include "mirror/object-inl.h"
30#include "scoped_thread_state_change.h"
31#include "thread-inl.h"
32#include "utils.h"
33
34namespace art {
35
Ian Rogerse63db272014-07-15 15:36:11 -070036CommonCompilerTest::CommonCompilerTest() {}
37CommonCompilerTest::~CommonCompilerTest() {}
38
Ian Rogerse63db272014-07-15 15:36:11 -070039void CommonCompilerTest::MakeExecutable(mirror::ArtMethod* method) {
40 CHECK(method != nullptr);
41
42 const CompiledMethod* compiled_method = nullptr;
43 if (!method->IsAbstract()) {
44 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
45 const DexFile& dex_file = *dex_cache->GetDexFile();
46 compiled_method =
47 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
48 method->GetDexMethodIndex()));
49 }
50 if (compiled_method != nullptr) {
51 const std::vector<uint8_t>* code = compiled_method->GetQuickCode();
52 const void* code_ptr;
Ian Rogers6f3dbba2014-10-14 17:41:57 -070053 bool is_portable = (code == nullptr);
54 if (!is_portable) {
Ian Rogerse63db272014-07-15 15:36:11 -070055 uint32_t code_size = code->size();
56 CHECK_NE(0u, code_size);
57 const std::vector<uint8_t>& vmap_table = compiled_method->GetVmapTable();
58 uint32_t vmap_table_offset = vmap_table.empty() ? 0u
59 : sizeof(OatQuickMethodHeader) + vmap_table.size();
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +000060 const std::vector<uint8_t>& mapping_table = *compiled_method->GetMappingTable();
Ian Rogerse63db272014-07-15 15:36:11 -070061 uint32_t mapping_table_offset = mapping_table.empty() ? 0u
62 : sizeof(OatQuickMethodHeader) + vmap_table.size() + mapping_table.size();
Mathieu Chartier957ca1c2014-11-21 16:51:29 -080063 const std::vector<uint8_t>& gc_map = *compiled_method->GetGcMap();
64 uint32_t gc_map_offset = gc_map.empty() ? 0u
65 : sizeof(OatQuickMethodHeader) + vmap_table.size() + mapping_table.size() + gc_map.size();
66 OatQuickMethodHeader method_header(mapping_table_offset, vmap_table_offset, gc_map_offset,
Ian Rogerse63db272014-07-15 15:36:11 -070067 compiled_method->GetFrameSizeInBytes(),
68 compiled_method->GetCoreSpillMask(),
69 compiled_method->GetFpSpillMask(), code_size);
70
71 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
72 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
Mathieu Chartier957ca1c2014-11-21 16:51:29 -080073 size_t size = sizeof(method_header) + code_size + vmap_table.size() + mapping_table.size() +
74 gc_map.size();
Ian Rogerse63db272014-07-15 15:36:11 -070075 size_t code_offset = compiled_method->AlignCode(size - code_size);
76 size_t padding = code_offset - (size - code_size);
77 chunk->reserve(padding + size);
78 chunk->resize(sizeof(method_header));
79 memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
80 chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end());
81 chunk->insert(chunk->begin(), mapping_table.begin(), mapping_table.end());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -080082 chunk->insert(chunk->begin(), gc_map.begin(), gc_map.end());
Ian Rogerse63db272014-07-15 15:36:11 -070083 chunk->insert(chunk->begin(), padding, 0);
84 chunk->insert(chunk->end(), code->begin(), code->end());
85 CHECK_EQ(padding + size, chunk->size());
86 code_ptr = &(*chunk)[code_offset];
87 } else {
88 code = compiled_method->GetPortableCode();
89 code_ptr = &(*code)[0];
90 }
91 MakeExecutable(code_ptr, code->size());
92 const void* method_code = CompiledMethod::CodePointer(code_ptr,
93 compiled_method->GetInstructionSet());
94 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
Ian Rogers6f3dbba2014-10-14 17:41:57 -070095 class_linker_->SetEntryPointsToCompiledCode(method, method_code, is_portable);
Ian Rogerse63db272014-07-15 15:36:11 -070096 } else {
97 // No code? You must mean to go into the interpreter.
98 // Or the generic JNI...
Ian Rogers6f3dbba2014-10-14 17:41:57 -070099 class_linker_->SetEntryPointsToInterpreter(method);
Ian Rogerse63db272014-07-15 15:36:11 -0700100 }
101}
102
103void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
104 CHECK(code_start != nullptr);
105 CHECK_NE(code_length, 0U);
106 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
107 uintptr_t base = RoundDown(data, kPageSize);
108 uintptr_t limit = RoundUp(data + code_length, kPageSize);
109 uintptr_t len = limit - base;
110 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
111 CHECK_EQ(result, 0);
112
113 // Flush instruction cache
114 // Only uses __builtin___clear_cache if GCC >= 4.3.3
115#if GCC_VERSION >= 40303
116 __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len));
117#else
118 // Only warn if not Intel as Intel doesn't have cache flush instructions.
119#if !defined(__i386__) && !defined(__x86_64__)
Ian Rogers2c4257b2014-10-24 14:20:06 -0700120 UNIMPLEMENTED(WARNING) << "cache flush";
Ian Rogerse63db272014-07-15 15:36:11 -0700121#endif
122#endif
123}
124
125void CommonCompilerTest::MakeExecutable(mirror::ClassLoader* class_loader, const char* class_name) {
126 std::string class_descriptor(DotToDescriptor(class_name));
127 Thread* self = Thread::Current();
128 StackHandleScope<1> hs(self);
129 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
130 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
131 CHECK(klass != nullptr) << "Class not found " << class_name;
132 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
133 MakeExecutable(klass->GetDirectMethod(i));
134 }
135 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
136 MakeExecutable(klass->GetVirtualMethod(i));
137 }
138}
139
140void CommonCompilerTest::SetUp() {
141 CommonRuntimeTest::SetUp();
142 {
143 ScopedObjectAccess soa(Thread::Current());
144
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700145 const InstructionSet instruction_set = kRuntimeISA;
Ian Rogerse63db272014-07-15 15:36:11 -0700146 // Take the default set of instruction features from the build.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700147 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
Ian Rogerse63db272014-07-15 15:36:11 -0700148
149 runtime_->SetInstructionSet(instruction_set);
150 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
151 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
152 if (!runtime_->HasCalleeSaveMethod(type)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700153 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
Ian Rogerse63db272014-07-15 15:36:11 -0700154 }
155 }
156
157 // TODO: make selectable
Ian Rogersd4c4d952014-10-16 20:31:53 -0700158 Compiler::Kind compiler_kind = kUsePortableCompiler ? Compiler::kPortable : Compiler::kQuick;
Ian Rogerse63db272014-07-15 15:36:11 -0700159 timer_.reset(new CumulativeLogger("Compilation times"));
160 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
161 verification_results_.get(),
162 method_inliner_map_.get(),
163 compiler_kind, instruction_set,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700164 instruction_set_features_.get(),
Andreas Gampe4bf3ae92014-11-11 13:28:29 -0800165 true, new std::set<std::string>, nullptr,
Ian Rogersd4c4d952014-10-16 20:31:53 -0700166 2, true, true, timer_.get(), ""));
Ian Rogerse63db272014-07-15 15:36:11 -0700167 }
168 // We typically don't generate an image in unit tests, disable this optimization by default.
169 compiler_driver_->SetSupportBootImageFixup(false);
170}
171
172void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
173 CommonRuntimeTest::SetUpRuntimeOptions(options);
174
175 compiler_options_.reset(new CompilerOptions);
176 verification_results_.reset(new VerificationResults(compiler_options_.get()));
177 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
178 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
179 method_inliner_map_.get()));
180 options->push_back(std::make_pair("compilercallbacks", callbacks_.get()));
181}
182
183void CommonCompilerTest::TearDown() {
184 timer_.reset();
185 compiler_driver_.reset();
186 callbacks_.reset();
187 method_inliner_map_.reset();
188 verification_results_.reset();
189 compiler_options_.reset();
190
191 CommonRuntimeTest::TearDown();
192}
193
194void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
195 std::string class_descriptor(DotToDescriptor(class_name));
196 Thread* self = Thread::Current();
197 StackHandleScope<1> hs(self);
198 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
199 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
200 CHECK(klass != nullptr) << "Class not found " << class_name;
201 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
202 CompileMethod(klass->GetDirectMethod(i));
203 }
204 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
205 CompileMethod(klass->GetVirtualMethod(i));
206 }
207}
208
209void CommonCompilerTest::CompileMethod(mirror::ArtMethod* method) {
210 CHECK(method != nullptr);
211 TimingLogger timings("CommonTest::CompileMethod", false, false);
212 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
213 compiler_driver_->CompileOne(method, &timings);
214 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
215 MakeExecutable(method);
216}
217
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700218void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
Ian Rogerse63db272014-07-15 15:36:11 -0700219 const char* class_name, const char* method_name,
220 const char* signature) {
221 std::string class_descriptor(DotToDescriptor(class_name));
222 Thread* self = Thread::Current();
223 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
224 CHECK(klass != nullptr) << "Class not found " << class_name;
225 mirror::ArtMethod* method = klass->FindDirectMethod(method_name, signature);
226 CHECK(method != nullptr) << "Direct method not found: "
227 << class_name << "." << method_name << signature;
228 CompileMethod(method);
229}
230
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700231void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700232 const char* class_name, const char* method_name,
233 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700234 std::string class_descriptor(DotToDescriptor(class_name));
235 Thread* self = Thread::Current();
236 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
237 CHECK(klass != nullptr) << "Class not found " << class_name;
238 mirror::ArtMethod* method = klass->FindVirtualMethod(method_name, signature);
239 CHECK(method != NULL) << "Virtual method not found: "
240 << class_name << "." << method_name << signature;
241 CompileMethod(method);
242}
243
244void CommonCompilerTest::ReserveImageSpace() {
245 // Reserve where the image will be loaded up front so that other parts of test set up don't
246 // accidentally end up colliding with the fixed memory address when we need to load the image.
247 std::string error_msg;
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700248 MemMap::Init();
Ian Rogerse63db272014-07-15 15:36:11 -0700249 image_reservation_.reset(MemMap::MapAnonymous("image reservation",
Ian Rogers13735952014-10-08 12:43:28 -0700250 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
Ian Rogerse63db272014-07-15 15:36:11 -0700251 (size_t)100 * 1024 * 1024, // 100MB
252 PROT_NONE,
253 false /* no need for 4gb flag with fixed mmap*/,
254 &error_msg));
255 CHECK(image_reservation_.get() != nullptr) << error_msg;
256}
257
258void CommonCompilerTest::UnreserveImageSpace() {
259 image_reservation_.reset();
260}
261
262} // namespace art