2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "common_compiler_test.h"
19 #include "arch/instruction_set_features.h"
20 #include "art_field-inl.h"
21 #include "art_method.h"
22 #include "class_linker.h"
23 #include "compiled_method.h"
24 #include "dex/quick_compiler_callbacks.h"
25 #include "dex/quick/dex_file_to_method_inliner_map.h"
26 #include "dex/verification_results.h"
27 #include "driver/compiler_driver.h"
28 #include "driver/compiler_options.h"
29 #include "interpreter/interpreter.h"
30 #include "mirror/class_loader.h"
31 #include "mirror/class-inl.h"
32 #include "mirror/dex_cache.h"
33 #include "mirror/object-inl.h"
34 #include "oat_quick_method_header.h"
35 #include "scoped_thread_state_change.h"
36 #include "thread-inl.h"
41 CommonCompilerTest::CommonCompilerTest() {}
42 CommonCompilerTest::~CommonCompilerTest() {}
44 void CommonCompilerTest::MakeExecutable(ArtMethod* method) {
45 CHECK(method != nullptr);
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();
52 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
53 method->GetDexMethodIndex()));
55 if (compiled_method != nullptr) {
56 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
57 uint32_t code_size = code.size();
58 CHECK_NE(0u, code_size);
59 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();
62 OatQuickMethodHeader method_header(vmap_table_offset,
63 compiled_method->GetFrameSizeInBytes(),
64 compiled_method->GetCoreSpillMask(),
65 compiled_method->GetFpSpillMask(),
68 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
69 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
70 const size_t max_padding = GetInstructionSetAlignment(compiled_method->GetInstructionSet());
71 const size_t size = vmap_table.size() + sizeof(method_header) + code_size;
72 chunk->reserve(size + max_padding);
73 chunk->resize(sizeof(method_header));
74 memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
75 chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end());
76 chunk->insert(chunk->end(), code.begin(), code.end());
77 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)));
86 MakeExecutable(code_ptr, code.size());
87 const void* method_code = CompiledMethod::CodePointer(code_ptr,
88 compiled_method->GetInstructionSet());
89 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
90 class_linker_->SetEntryPointsToCompiledCode(method, method_code);
92 // No code? You must mean to go into the interpreter.
93 // Or the generic JNI...
94 class_linker_->SetEntryPointsToInterpreter(method);
98 void 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);
108 FlushInstructionCache(reinterpret_cast<char*>(base), reinterpret_cast<char*>(base + len));
111 void 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;
118 size_t pointer_size = class_linker_->GetImagePointerSize();
119 for (auto& m : klass->GetMethods(pointer_size)) {
124 // 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.
126 std::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>();
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.
133 std::unordered_set<std::string>* CommonCompilerTest::GetCompiledClasses() {
134 // Null, no selection of compiled-classes.
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.
140 std::unordered_set<std::string>* CommonCompilerTest::GetCompiledMethods() {
141 // Null, no selection of compiled-methods.
145 // Get ProfileCompilationInfo that should be passed to the driver.
146 ProfileCompilationInfo* CommonCompilerTest::GetProfileCompilationInfo() {
147 // Null, profile information will not be taken into account.
151 void CommonCompilerTest::SetUp() {
152 CommonRuntimeTest::SetUp();
154 ScopedObjectAccess soa(Thread::Current());
156 const InstructionSet instruction_set = kRuntimeISA;
157 // Take the default set of instruction features from the build.
158 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
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)) {
164 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
168 timer_.reset(new CumulativeLogger("Compilation times"));
169 CreateCompilerDriver(compiler_kind_, instruction_set);
173 void CommonCompilerTest::CreateCompilerDriver(Compiler::Kind kind,
175 size_t number_of_threads) {
176 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
177 verification_results_.get(),
178 method_inliner_map_.get(),
181 instruction_set_features_.get(),
182 /* boot_image */ true,
183 /* app_image */ false,
185 GetCompiledClasses(),
186 GetCompiledMethods(),
188 /* dump_stats */ true,
189 /* dump_passes */ true,
192 GetProfileCompilationInfo()));
193 // We typically don't generate an image in unit tests, disable this optimization by default.
194 compiler_driver_->SetSupportBootImageFixup(false);
197 void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
198 CommonRuntimeTest::SetUpRuntimeOptions(options);
200 compiler_options_.reset(new CompilerOptions);
201 verification_results_.reset(new VerificationResults(compiler_options_.get()));
202 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
203 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
204 method_inliner_map_.get(),
205 CompilerCallbacks::CallbackMode::kCompileApp));
208 Compiler::Kind CommonCompilerTest::GetCompilerKind() const {
209 return compiler_kind_;
212 void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) {
213 compiler_kind_ = compiler_kind;
216 InstructionSet CommonCompilerTest::GetInstructionSet() const {
217 DCHECK(compiler_driver_.get() != nullptr);
218 return compiler_driver_->GetInstructionSet();
221 void CommonCompilerTest::TearDown() {
223 compiler_driver_.reset();
225 method_inliner_map_.reset();
226 verification_results_.reset();
227 compiler_options_.reset();
228 image_reservation_.reset();
230 CommonRuntimeTest::TearDown();
233 void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
234 std::string class_descriptor(DotToDescriptor(class_name));
235 Thread* self = Thread::Current();
236 StackHandleScope<1> hs(self);
237 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
238 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
239 CHECK(klass != nullptr) << "Class not found " << class_name;
240 auto pointer_size = class_linker_->GetImagePointerSize();
241 for (auto& m : klass->GetMethods(pointer_size)) {
246 void CommonCompilerTest::CompileMethod(ArtMethod* method) {
247 CHECK(method != nullptr);
248 TimingLogger timings("CommonTest::CompileMethod", false, false);
249 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
250 compiler_driver_->CompileOne(Thread::Current(), method, &timings);
251 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
252 MakeExecutable(method);
255 void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
256 const char* class_name, const char* method_name,
257 const char* signature) {
258 std::string class_descriptor(DotToDescriptor(class_name));
259 Thread* self = Thread::Current();
260 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
261 CHECK(klass != nullptr) << "Class not found " << class_name;
262 auto pointer_size = class_linker_->GetImagePointerSize();
263 ArtMethod* method = klass->FindDirectMethod(method_name, signature, pointer_size);
264 CHECK(method != nullptr) << "Direct method not found: "
265 << class_name << "." << method_name << signature;
266 CompileMethod(method);
269 void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
270 const char* class_name, const char* method_name,
271 const char* signature) {
272 std::string class_descriptor(DotToDescriptor(class_name));
273 Thread* self = Thread::Current();
274 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
275 CHECK(klass != nullptr) << "Class not found " << class_name;
276 auto pointer_size = class_linker_->GetImagePointerSize();
277 ArtMethod* method = klass->FindVirtualMethod(method_name, signature, pointer_size);
278 CHECK(method != nullptr) << "Virtual method not found: "
279 << class_name << "." << method_name << signature;
280 CompileMethod(method);
283 void CommonCompilerTest::ReserveImageSpace() {
284 // Reserve where the image will be loaded up front so that other parts of test set up don't
285 // accidentally end up colliding with the fixed memory address when we need to load the image.
286 std::string error_msg;
288 image_reservation_.reset(MemMap::MapAnonymous("image reservation",
289 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
290 (size_t)120 * 1024 * 1024, // 120MB
292 false /* no need for 4gb flag with fixed mmap*/,
293 false /* not reusing existing reservation */,
295 CHECK(image_reservation_.get() != nullptr) << error_msg;
298 void CommonCompilerTest::UnreserveImageSpace() {
299 image_reservation_.reset();