1 /*
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
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/pass_manager.h"
25 #include "dex/quick_compiler_callbacks.h"
26 #include "dex/quick/dex_file_to_method_inliner_map.h"
27 #include "dex/verification_results.h"
28 #include "driver/compiler_driver.h"
29 #include "driver/compiler_options.h"
30 #include "interpreter/interpreter.h"
31 #include "mirror/class_loader.h"
32 #include "mirror/class-inl.h"
33 #include "mirror/dex_cache.h"
34 #include "mirror/object-inl.h"
35 #include "scoped_thread_state_change.h"
36 #include "thread-inl.h"
37 #include "utils.h"
38
39 namespace art {
40
CommonCompilerTest()41 CommonCompilerTest::CommonCompilerTest() {}
~CommonCompilerTest()42 CommonCompilerTest::~CommonCompilerTest() {}
43
MakeExecutable(ArtMethod * method)44 void CommonCompilerTest::MakeExecutable(ArtMethod* method) {
45 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) {
56 const SwapVector<uint8_t>* code = compiled_method->GetQuickCode();
57 uint32_t code_size = code->size();
58 CHECK_NE(0u, code_size);
59 const SwapVector<uint8_t>* vmap_table = compiled_method->GetVmapTable();
60 uint32_t vmap_table_offset = vmap_table->empty() ? 0u
61 : sizeof(OatQuickMethodHeader) + vmap_table->size();
62 const SwapVector<uint8_t>* mapping_table = compiled_method->GetMappingTable();
63 bool mapping_table_used = mapping_table != nullptr && !mapping_table->empty();
64 size_t mapping_table_size = mapping_table_used ? mapping_table->size() : 0U;
65 uint32_t mapping_table_offset = !mapping_table_used ? 0u
66 : sizeof(OatQuickMethodHeader) + vmap_table->size() + mapping_table_size;
67 const SwapVector<uint8_t>* gc_map = compiled_method->GetGcMap();
68 bool gc_map_used = gc_map != nullptr && !gc_map->empty();
69 size_t gc_map_size = gc_map_used ? gc_map->size() : 0U;
70 uint32_t gc_map_offset = !gc_map_used ? 0u
71 : sizeof(OatQuickMethodHeader) + vmap_table->size() + mapping_table_size + gc_map_size;
72 OatQuickMethodHeader method_header(mapping_table_offset, vmap_table_offset, gc_map_offset,
73 compiled_method->GetFrameSizeInBytes(),
74 compiled_method->GetCoreSpillMask(),
75 compiled_method->GetFpSpillMask(), code_size);
76
77 header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
78 std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
79 size_t size = sizeof(method_header) + code_size + vmap_table->size() + mapping_table_size +
80 gc_map_size;
81 size_t code_offset = compiled_method->AlignCode(size - code_size);
82 size_t padding = code_offset - (size - code_size);
83 chunk->reserve(padding + size);
84 chunk->resize(sizeof(method_header));
85 memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
86 chunk->insert(chunk->begin(), vmap_table->begin(), vmap_table->end());
87 if (mapping_table_used) {
88 chunk->insert(chunk->begin(), mapping_table->begin(), mapping_table->end());
89 }
90 if (gc_map_used) {
91 chunk->insert(chunk->begin(), gc_map->begin(), gc_map->end());
92 }
93 chunk->insert(chunk->begin(), padding, 0);
94 chunk->insert(chunk->end(), code->begin(), code->end());
95 CHECK_EQ(padding + size, chunk->size());
96 const void* code_ptr = &(*chunk)[code_offset];
97 MakeExecutable(code_ptr, code->size());
98 const void* method_code = CompiledMethod::CodePointer(code_ptr,
99 compiled_method->GetInstructionSet());
100 LOG(INFO) << "MakeExecutable " << PrettyMethod(method) << " code=" << method_code;
101 class_linker_->SetEntryPointsToCompiledCode(method, method_code);
102 } else {
103 // No code? You must mean to go into the interpreter.
104 // Or the generic JNI...
105 class_linker_->SetEntryPointsToInterpreter(method);
106 }
107 }
108
MakeExecutable(const void * code_start,size_t code_length)109 void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
110 CHECK(code_start != nullptr);
111 CHECK_NE(code_length, 0U);
112 uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
113 uintptr_t base = RoundDown(data, kPageSize);
114 uintptr_t limit = RoundUp(data + code_length, kPageSize);
115 uintptr_t len = limit - base;
116 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
117 CHECK_EQ(result, 0);
118
119 // Flush instruction cache
120 // Only uses __builtin___clear_cache if GCC >= 4.3.3
121 #if GCC_VERSION >= 40303
122 __builtin___clear_cache(reinterpret_cast<void*>(base), reinterpret_cast<void*>(base + len));
123 #else
124 // Only warn if not Intel as Intel doesn't have cache flush instructions.
125 #if !defined(__i386__) && !defined(__x86_64__)
126 UNIMPLEMENTED(WARNING) << "cache flush";
127 #endif
128 #endif
129 }
130
MakeExecutable(mirror::ClassLoader * class_loader,const char * class_name)131 void CommonCompilerTest::MakeExecutable(mirror::ClassLoader* class_loader, const char* class_name) {
132 std::string class_descriptor(DotToDescriptor(class_name));
133 Thread* self = Thread::Current();
134 StackHandleScope<1> hs(self);
135 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
136 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
137 CHECK(klass != nullptr) << "Class not found " << class_name;
138 size_t pointer_size = class_linker_->GetImagePointerSize();
139 for (auto& m : klass->GetDirectMethods(pointer_size)) {
140 MakeExecutable(&m);
141 }
142 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
143 MakeExecutable(&m);
144 }
145 }
146
147 // Get the set of image classes given to the compiler-driver in SetUp. Note: the compiler
148 // driver assumes ownership of the set, so the test should properly release the set.
GetImageClasses()149 std::unordered_set<std::string>* CommonCompilerTest::GetImageClasses() {
150 // Empty set: by default no classes are retained in the image.
151 return new std::unordered_set<std::string>();
152 }
153
154 // Get the set of compiled classes given to the compiler-driver in SetUp. Note: the compiler
155 // driver assumes ownership of the set, so the test should properly release the set.
GetCompiledClasses()156 std::unordered_set<std::string>* CommonCompilerTest::GetCompiledClasses() {
157 // Null, no selection of compiled-classes.
158 return nullptr;
159 }
160
161 // Get the set of compiled methods given to the compiler-driver in SetUp. Note: the compiler
162 // driver assumes ownership of the set, so the test should properly release the set.
GetCompiledMethods()163 std::unordered_set<std::string>* CommonCompilerTest::GetCompiledMethods() {
164 // Null, no selection of compiled-methods.
165 return nullptr;
166 }
167
SetUp()168 void CommonCompilerTest::SetUp() {
169 CommonRuntimeTest::SetUp();
170 {
171 ScopedObjectAccess soa(Thread::Current());
172
173 const InstructionSet instruction_set = kRuntimeISA;
174 // Take the default set of instruction features from the build.
175 instruction_set_features_.reset(InstructionSetFeatures::FromCppDefines());
176
177 runtime_->SetInstructionSet(instruction_set);
178 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
179 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
180 if (!runtime_->HasCalleeSaveMethod(type)) {
181 runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
182 }
183 }
184
185 // TODO: make selectable
186 Compiler::Kind compiler_kind = Compiler::kQuick;
187 timer_.reset(new CumulativeLogger("Compilation times"));
188 compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
189 verification_results_.get(),
190 method_inliner_map_.get(),
191 compiler_kind, instruction_set,
192 instruction_set_features_.get(),
193 true,
194 GetImageClasses(),
195 GetCompiledClasses(),
196 GetCompiledMethods(),
197 2, true, true, "", timer_.get(), -1, ""));
198 }
199 // We typically don't generate an image in unit tests, disable this optimization by default.
200 compiler_driver_->SetSupportBootImageFixup(false);
201 }
202
SetUpRuntimeOptions(RuntimeOptions * options)203 void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
204 CommonRuntimeTest::SetUpRuntimeOptions(options);
205
206 compiler_options_.reset(new CompilerOptions);
207 verification_results_.reset(new VerificationResults(compiler_options_.get()));
208 method_inliner_map_.reset(new DexFileToMethodInlinerMap);
209 callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
210 method_inliner_map_.get(),
211 CompilerCallbacks::CallbackMode::kCompileApp));
212 }
213
TearDown()214 void CommonCompilerTest::TearDown() {
215 timer_.reset();
216 compiler_driver_.reset();
217 callbacks_.reset();
218 method_inliner_map_.reset();
219 verification_results_.reset();
220 compiler_options_.reset();
221
222 CommonRuntimeTest::TearDown();
223 }
224
CompileClass(mirror::ClassLoader * class_loader,const char * class_name)225 void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
226 std::string class_descriptor(DotToDescriptor(class_name));
227 Thread* self = Thread::Current();
228 StackHandleScope<1> hs(self);
229 Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
230 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
231 CHECK(klass != nullptr) << "Class not found " << class_name;
232 auto pointer_size = class_linker_->GetImagePointerSize();
233 for (auto& m : klass->GetDirectMethods(pointer_size)) {
234 CompileMethod(&m);
235 }
236 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
237 CompileMethod(&m);
238 }
239 }
240
CompileMethod(ArtMethod * method)241 void CommonCompilerTest::CompileMethod(ArtMethod* method) {
242 CHECK(method != nullptr);
243 TimingLogger timings("CommonTest::CompileMethod", false, false);
244 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
245 compiler_driver_->CompileOne(Thread::Current(), method, &timings);
246 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
247 MakeExecutable(method);
248 }
249
CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,const char * class_name,const char * method_name,const char * signature)250 void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
251 const char* class_name, const char* method_name,
252 const char* signature) {
253 std::string class_descriptor(DotToDescriptor(class_name));
254 Thread* self = Thread::Current();
255 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
256 CHECK(klass != nullptr) << "Class not found " << class_name;
257 auto pointer_size = class_linker_->GetImagePointerSize();
258 ArtMethod* method = klass->FindDirectMethod(method_name, signature, pointer_size);
259 CHECK(method != nullptr) << "Direct method not found: "
260 << class_name << "." << method_name << signature;
261 CompileMethod(method);
262 }
263
CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,const char * class_name,const char * method_name,const char * signature)264 void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
265 const char* class_name, const char* method_name,
266 const char* signature) {
267 std::string class_descriptor(DotToDescriptor(class_name));
268 Thread* self = Thread::Current();
269 mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
270 CHECK(klass != nullptr) << "Class not found " << class_name;
271 auto pointer_size = class_linker_->GetImagePointerSize();
272 ArtMethod* method = klass->FindVirtualMethod(method_name, signature, pointer_size);
273 CHECK(method != nullptr) << "Virtual method not found: "
274 << class_name << "." << method_name << signature;
275 CompileMethod(method);
276 }
277
ReserveImageSpace()278 void CommonCompilerTest::ReserveImageSpace() {
279 // Reserve where the image will be loaded up front so that other parts of test set up don't
280 // accidentally end up colliding with the fixed memory address when we need to load the image.
281 std::string error_msg;
282 MemMap::Init();
283 image_reservation_.reset(MemMap::MapAnonymous("image reservation",
284 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
285 (size_t)100 * 1024 * 1024, // 100MB
286 PROT_NONE,
287 false /* no need for 4gb flag with fixed mmap*/,
288 false /* not reusing existing reservation */,
289 &error_msg));
290 CHECK(image_reservation_.get() != nullptr) << error_msg;
291 }
292
UnreserveImageSpace()293 void CommonCompilerTest::UnreserveImageSpace() {
294 image_reservation_.reset();
295 }
296
297 } // namespace art
298