blob: 8ffc86ea3f4c65ec914ca3fd60888bc5b4bf7a86 [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"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080022#include "dex/pass_manager.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "dex/quick_compiler_callbacks.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "dex/quick/dex_file_to_method_inliner_map.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080025#include "dex/verification_results.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000027#include "driver/compiler_options.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "interpreter/interpreter.h"
29#include "mirror/art_method.h"
30#include "mirror/dex_cache.h"
31#include "mirror/object-inl.h"
32#include "scoped_thread_state_change.h"
33#include "thread-inl.h"
34#include "utils.h"
35
36namespace art {
37
Ian Rogerse63db272014-07-15 15:36:11 -070038CommonCompilerTest::CommonCompilerTest() {}
39CommonCompilerTest::~CommonCompilerTest() {}
40
Ian Rogerse63db272014-07-15 15:36:11 -070041void CommonCompilerTest::MakeExecutable(mirror::ArtMethod* method) {
42 CHECK(method != nullptr);
43
44 const CompiledMethod* compiled_method = nullptr;
45 if (!method->IsAbstract()) {
46 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
47 const DexFile& dex_file = *dex_cache->GetDexFile();
48 compiled_method =
49 compiler_driver_->GetCompiledMethod(MethodReference(&dex_file,
50 method->GetDexMethodIndex()));
51 }
52 if (compiled_method != nullptr) {
Andreas Gampee21dc3d2014-12-08 16:59:43 -080053 const SwapVector<uint8_t>* code = compiled_method->GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -080054 uint32_t code_size = code->size();
55 CHECK_NE(0u, code_size);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080056 const SwapVector<uint8_t>* vmap_table = compiled_method->GetVmapTable();
57 uint32_t vmap_table_offset = vmap_table->empty() ? 0u
58 : sizeof(OatQuickMethodHeader) + vmap_table->size();
Andreas Gampea2d0afc2014-12-22 13:43:33 -080059 const SwapVector<uint8_t>* mapping_table = compiled_method->GetMappingTable();
60 bool mapping_table_used = mapping_table != nullptr && !mapping_table->empty();
61 size_t mapping_table_size = mapping_table_used ? mapping_table->size() : 0U;
62 uint32_t mapping_table_offset = !mapping_table_used ? 0u
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080063 : sizeof(OatQuickMethodHeader) + vmap_table->size() + mapping_table_size;
Andreas Gampea2d0afc2014-12-22 13:43:33 -080064 const SwapVector<uint8_t>* gc_map = compiled_method->GetGcMap();
65 bool gc_map_used = gc_map != nullptr && !gc_map->empty();
66 size_t gc_map_size = gc_map_used ? gc_map->size() : 0U;
67 uint32_t gc_map_offset = !gc_map_used ? 0u
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080068 : sizeof(OatQuickMethodHeader) + vmap_table->size() + mapping_table_size + gc_map_size;
Elliott Hughes956af0f2014-12-11 14:34:28 -080069 OatQuickMethodHeader method_header(mapping_table_offset, vmap_table_offset, gc_map_offset,
70 compiled_method->GetFrameSizeInBytes(),
71 compiled_method->GetCoreSpillMask(),
72 compiled_method->GetFpSpillMask(), code_size);
Ian Rogerse63db272014-07-15 15:36:11 -070073
Elliott Hughes956af0f2014-12-11 14:34:28 -080074 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
75 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080076 size_t size = sizeof(method_header) + code_size + vmap_table->size() + mapping_table_size +
Andreas Gampea2d0afc2014-12-22 13:43:33 -080077 gc_map_size;
Elliott Hughes956af0f2014-12-11 14:34:28 -080078 size_t code_offset = compiled_method->AlignCode(size - code_size);
79 size_t padding = code_offset - (size - code_size);
80 chunk->reserve(padding + size);
81 chunk->resize(sizeof(method_header));
82 memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080083 chunk->insert(chunk->begin(), vmap_table->begin(), vmap_table->end());
Andreas Gampea2d0afc2014-12-22 13:43:33 -080084 if (mapping_table_used) {
85 chunk->insert(chunk->begin(), mapping_table->begin(), mapping_table->end());
86 }
87 if (gc_map_used) {
88 chunk->insert(chunk->begin(), gc_map->begin(), gc_map->end());
89 }
Elliott Hughes956af0f2014-12-11 14:34:28 -080090 chunk->insert(chunk->begin(), padding, 0);
91 chunk->insert(chunk->end(), code->begin(), code->end());
92 CHECK_EQ(padding + size, chunk->size());
93 const void* code_ptr = &(*chunk)[code_offset];
Ian Rogerse63db272014-07-15 15:36:11 -070094 MakeExecutable(code_ptr, code->size());
95 const void* method_code = CompiledMethod::CodePointer(code_ptr,
96 compiled_method->GetInstructionSet());
97 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
Elliott Hughes956af0f2014-12-11 14:34:28 -080098 class_linker_->SetEntryPointsToCompiledCode(method, method_code);
Ian Rogerse63db272014-07-15 15:36:11 -070099 } else {
100 // No code? You must mean to go into the interpreter.
101 // Or the generic JNI...
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700102 class_linker_->SetEntryPointsToInterpreter(method);
Ian Rogerse63db272014-07-15 15:36:11 -0700103 }
104}
105
106void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
107 CHECK(code_start != nullptr);
108 CHECK_NE(code_length, 0U);
109 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
110 uintptr_t base = RoundDown(data, kPageSize);
111 uintptr_t limit = RoundUp(data + code_length, kPageSize);
112 uintptr_t len = limit - base;
113 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
114 CHECK_EQ(result, 0);
115
116 // Flush instruction cache
117 // Only uses __builtin___clear_cache if GCC >= 4.3.3
118#if GCC_VERSION >= 40303
119 __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len));
120#else
121 // Only warn if not Intel as Intel doesn't have cache flush instructions.
122#if !defined(__i386__) && !defined(__x86_64__)
Ian Rogers2c4257b2014-10-24 14:20:06 -0700123 UNIMPLEMENTED(WARNING) << "cache flush";
Ian Rogerse63db272014-07-15 15:36:11 -0700124#endif
125#endif
126}
127
128void CommonCompilerTest::MakeExecutable(mirror::ClassLoader* class_loader, const char* class_name) {
129 std::string class_descriptor(DotToDescriptor(class_name));
130 Thread* self = Thread::Current();
131 StackHandleScope<1> hs(self);
132 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
133 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
134 CHECK(klass != nullptr) << "Class not found " << class_name;
135 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
136 MakeExecutable(klass->GetDirectMethod(i));
137 }
138 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
139 MakeExecutable(klass->GetVirtualMethod(i));
140 }
141}
142
143void CommonCompilerTest::SetUp() {
144 CommonRuntimeTest::SetUp();
145 {
146 ScopedObjectAccess soa(Thread::Current());
147
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700148 const InstructionSet instruction_set = kRuntimeISA;
Ian Rogerse63db272014-07-15 15:36:11 -0700149 // Take the default set of instruction features from the build.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700150 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
Ian Rogerse63db272014-07-15 15:36:11 -0700151
152 runtime_->SetInstructionSet(instruction_set);
153 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
154 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
155 if (!runtime_->HasCalleeSaveMethod(type)) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700156 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
Ian Rogerse63db272014-07-15 15:36:11 -0700157 }
158 }
159
160 // TODO: make selectable
Elliott Hughes956af0f2014-12-11 14:34:28 -0800161 Compiler::Kind compiler_kind = Compiler::kQuick;
Ian Rogerse63db272014-07-15 15:36:11 -0700162 timer_.reset(new CumulativeLogger("Compilation times"));
163 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
164 verification_results_.get(),
165 method_inliner_map_.get(),
166 compiler_kind, instruction_set,
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700167 instruction_set_features_.get(),
Andreas Gampeb1fcead2015-04-20 18:53:51 -0700168 true, new std::unordered_set<std::string>, nullptr,
David Brazdil866c0312015-01-13 21:21:31 +0000169 2, true, true, "", timer_.get(), -1, ""));
Ian Rogerse63db272014-07-15 15:36:11 -0700170 }
171 // We typically don't generate an image in unit tests, disable this optimization by default.
172 compiler_driver_->SetSupportBootImageFixup(false);
173}
174
175void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
176 CommonRuntimeTest::SetUpRuntimeOptions(options);
177
178 compiler_options_.reset(new CompilerOptions);
179 verification_results_.reset(new VerificationResults(compiler_options_.get()));
180 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
181 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700182 method_inliner_map_.get(),
Andreas Gampe4585f872015-03-27 23:45:15 -0700183 CompilerCallbacks::CallbackMode::kCompileApp));
Ian Rogerse63db272014-07-15 15:36:11 -0700184}
185
186void CommonCompilerTest::TearDown() {
187 timer_.reset();
188 compiler_driver_.reset();
189 callbacks_.reset();
190 method_inliner_map_.reset();
191 verification_results_.reset();
192 compiler_options_.reset();
193
194 CommonRuntimeTest::TearDown();
195}
196
197void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
198 std::string class_descriptor(DotToDescriptor(class_name));
199 Thread* self = Thread::Current();
200 StackHandleScope<1> hs(self);
201 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
202 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
203 CHECK(klass != nullptr) << "Class not found " << class_name;
204 for (size_t i = 0; i < klass->NumDirectMethods(); i++) {
205 CompileMethod(klass->GetDirectMethod(i));
206 }
207 for (size_t i = 0; i < klass->NumVirtualMethods(); i++) {
208 CompileMethod(klass->GetVirtualMethod(i));
209 }
210}
211
212void CommonCompilerTest::CompileMethod(mirror::ArtMethod* method) {
213 CHECK(method != nullptr);
214 TimingLogger timings("CommonTest::CompileMethod", false, false);
215 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800216 compiler_driver_->CompileOne(Thread::Current(), method, &timings);
Ian Rogerse63db272014-07-15 15:36:11 -0700217 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
218 MakeExecutable(method);
219}
220
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700221void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
Ian Rogerse63db272014-07-15 15:36:11 -0700222 const char* class_name, const char* method_name,
223 const char* signature) {
224 std::string class_descriptor(DotToDescriptor(class_name));
225 Thread* self = Thread::Current();
226 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
227 CHECK(klass != nullptr) << "Class not found " << class_name;
228 mirror::ArtMethod* method = klass->FindDirectMethod(method_name, signature);
229 CHECK(method != nullptr) << "Direct method not found: "
230 << class_name << "." << method_name << signature;
231 CompileMethod(method);
232}
233
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700234void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700235 const char* class_name, const char* method_name,
236 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700237 std::string class_descriptor(DotToDescriptor(class_name));
238 Thread* self = Thread::Current();
239 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
240 CHECK(klass != nullptr) << "Class not found " << class_name;
241 mirror::ArtMethod* method = klass->FindVirtualMethod(method_name, signature);
242 CHECK(method != NULL) << "Virtual method not found: "
243 << class_name << "." << method_name << signature;
244 CompileMethod(method);
245}
246
247void CommonCompilerTest::ReserveImageSpace() {
248 // Reserve where the image will be loaded up front so that other parts of test set up don't
249 // accidentally end up colliding with the fixed memory address when we need to load the image.
250 std::string error_msg;
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700251 MemMap::Init();
Ian Rogerse63db272014-07-15 15:36:11 -0700252 image_reservation_.reset(MemMap::MapAnonymous("image reservation",
Ian Rogers13735952014-10-08 12:43:28 -0700253 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
Ian Rogerse63db272014-07-15 15:36:11 -0700254 (size_t)100 * 1024 * 1024, // 100MB
255 PROT_NONE,
256 false /* no need for 4gb flag with fixed mmap*/,
Vladimir Marko5c42c292015-02-25 12:02:49 +0000257 false /* not reusing existing reservation */,
Ian Rogerse63db272014-07-15 15:36:11 -0700258 &error_msg));
259 CHECK(image_reservation_.get() != nullptr) << error_msg;
260}
261
262void CommonCompilerTest::UnreserveImageSpace() {
263 image_reservation_.reset();
264}
265
266} // namespace art