Home | History | Annotate | Download | only in compiler
      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-inl.h"
     22 #include "base/enums.h"
     23 #include "class_linker.h"
     24 #include "compiled_method.h"
     25 #include "dex/quick_compiler_callbacks.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-inl.h"
     36 #include "thread-inl.h"
     37 #include "utils.h"
     38 
     39 namespace art {
     40 
     41 CommonCompilerTest::CommonCompilerTest() {}
     42 CommonCompilerTest::~CommonCompilerTest() {}
     43 
     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 the code size is 0 it means the method was skipped due to profile guided compilation.
     56   if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0u) {
     57     ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
     58     const uint32_t code_size = code.size();
     59     ArrayRef<const uint8_t> vmap_table = compiled_method->GetVmapTable();
     60     const uint32_t vmap_table_offset = vmap_table.empty() ? 0u
     61         : sizeof(OatQuickMethodHeader) + vmap_table.size();
     62     // The method info is directly before the vmap table.
     63     ArrayRef<const uint8_t> method_info = compiled_method->GetMethodInfo();
     64     const uint32_t method_info_offset = method_info.empty() ? 0u
     65         : vmap_table_offset + method_info.size();
     66 
     67     OatQuickMethodHeader method_header(vmap_table_offset,
     68                                        method_info_offset,
     69                                        compiled_method->GetFrameSizeInBytes(),
     70                                        compiled_method->GetCoreSpillMask(),
     71                                        compiled_method->GetFpSpillMask(),
     72                                        code_size);
     73 
     74     header_code_and_maps_chunks_.push_back(std::vector<uint8_t>());
     75     std::vector<uint8_t>* chunk = &header_code_and_maps_chunks_.back();
     76     const size_t max_padding = GetInstructionSetAlignment(compiled_method->GetInstructionSet());
     77     const size_t size = method_info.size() + vmap_table.size() + sizeof(method_header) + code_size;
     78     chunk->reserve(size + max_padding);
     79     chunk->resize(sizeof(method_header));
     80     memcpy(&(*chunk)[0], &method_header, sizeof(method_header));
     81     chunk->insert(chunk->begin(), vmap_table.begin(), vmap_table.end());
     82     chunk->insert(chunk->begin(), method_info.begin(), method_info.end());
     83     chunk->insert(chunk->end(), code.begin(), code.end());
     84     CHECK_EQ(chunk->size(), size);
     85     const void* unaligned_code_ptr = chunk->data() + (size - code_size);
     86     size_t offset = dchecked_integral_cast<size_t>(reinterpret_cast<uintptr_t>(unaligned_code_ptr));
     87     size_t padding = compiled_method->AlignCode(offset) - offset;
     88     // Make sure no resizing takes place.
     89     CHECK_GE(chunk->capacity(), chunk->size() + padding);
     90     chunk->insert(chunk->begin(), padding, 0);
     91     const void* code_ptr = reinterpret_cast<const uint8_t*>(unaligned_code_ptr) + padding;
     92     CHECK_EQ(code_ptr, static_cast<const void*>(chunk->data() + (chunk->size() - code_size)));
     93     MakeExecutable(code_ptr, code.size());
     94     const void* method_code = CompiledMethod::CodePointer(code_ptr,
     95                                                           compiled_method->GetInstructionSet());
     96     LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code;
     97     class_linker_->SetEntryPointsToCompiledCode(method, method_code);
     98   } else {
     99     // No code? You must mean to go into the interpreter.
    100     // Or the generic JNI...
    101     class_linker_->SetEntryPointsToInterpreter(method);
    102   }
    103 }
    104 
    105 void CommonCompilerTest::MakeExecutable(const void* code_start, size_t code_length) {
    106   CHECK(code_start != nullptr);
    107   CHECK_NE(code_length, 0U);
    108   uintptr_t data = reinterpret_cast<uintptr_t>(code_start);
    109   uintptr_t base = RoundDown(data, kPageSize);
    110   uintptr_t limit = RoundUp(data + code_length, kPageSize);
    111   uintptr_t len = limit - base;
    112   int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
    113   CHECK_EQ(result, 0);
    114 
    115   FlushInstructionCache(reinterpret_cast<char*>(base), reinterpret_cast<char*>(base + len));
    116 }
    117 
    118 void CommonCompilerTest::MakeExecutable(ObjPtr<mirror::ClassLoader> class_loader,
    119                                         const char* class_name) {
    120   std::string class_descriptor(DotToDescriptor(class_name));
    121   Thread* self = Thread::Current();
    122   StackHandleScope<1> hs(self);
    123   Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
    124   mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
    125   CHECK(klass != nullptr) << "Class not found " << class_name;
    126   PointerSize pointer_size = class_linker_->GetImagePointerSize();
    127   for (auto& m : klass->GetMethods(pointer_size)) {
    128     MakeExecutable(&m);
    129   }
    130 }
    131 
    132 // Get the set of image classes given to the compiler-driver in SetUp. Note: the compiler
    133 // driver assumes ownership of the set, so the test should properly release the set.
    134 std::unordered_set<std::string>* CommonCompilerTest::GetImageClasses() {
    135   // Empty set: by default no classes are retained in the image.
    136   return new std::unordered_set<std::string>();
    137 }
    138 
    139 // Get the set of compiled classes given to the compiler-driver in SetUp. Note: the compiler
    140 // driver assumes ownership of the set, so the test should properly release the set.
    141 std::unordered_set<std::string>* CommonCompilerTest::GetCompiledClasses() {
    142   // Null, no selection of compiled-classes.
    143   return nullptr;
    144 }
    145 
    146 // Get the set of compiled methods given to the compiler-driver in SetUp. Note: the compiler
    147 // driver assumes ownership of the set, so the test should properly release the set.
    148 std::unordered_set<std::string>* CommonCompilerTest::GetCompiledMethods() {
    149   // Null, no selection of compiled-methods.
    150   return nullptr;
    151 }
    152 
    153 // Get ProfileCompilationInfo that should be passed to the driver.
    154 ProfileCompilationInfo* CommonCompilerTest::GetProfileCompilationInfo() {
    155   // Null, profile information will not be taken into account.
    156   return nullptr;
    157 }
    158 
    159 void CommonCompilerTest::SetUp() {
    160   CommonRuntimeTest::SetUp();
    161   {
    162     ScopedObjectAccess soa(Thread::Current());
    163 
    164     const InstructionSet instruction_set = kRuntimeISA;
    165     // Take the default set of instruction features from the build.
    166     instruction_set_features_ = InstructionSetFeatures::FromCppDefines();
    167 
    168     runtime_->SetInstructionSet(instruction_set);
    169     for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
    170       Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
    171       if (!runtime_->HasCalleeSaveMethod(type)) {
    172         runtime_->SetCalleeSaveMethod(runtime_->CreateCalleeSaveMethod(), type);
    173       }
    174     }
    175 
    176     timer_.reset(new CumulativeLogger("Compilation times"));
    177     CreateCompilerDriver(compiler_kind_, instruction_set);
    178   }
    179 }
    180 
    181 void CommonCompilerTest::CreateCompilerDriver(Compiler::Kind kind,
    182                                               InstructionSet isa,
    183                                               size_t number_of_threads) {
    184   compiler_options_->boot_image_ = true;
    185   compiler_options_->SetCompilerFilter(GetCompilerFilter());
    186   compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
    187                                             verification_results_.get(),
    188                                             kind,
    189                                             isa,
    190                                             instruction_set_features_.get(),
    191                                             GetImageClasses(),
    192                                             GetCompiledClasses(),
    193                                             GetCompiledMethods(),
    194                                             number_of_threads,
    195                                             /* dump_stats */ true,
    196                                             /* dump_passes */ true,
    197                                             timer_.get(),
    198                                             /* swap_fd */ -1,
    199                                             GetProfileCompilationInfo()));
    200   // We typically don't generate an image in unit tests, disable this optimization by default.
    201   compiler_driver_->SetSupportBootImageFixup(false);
    202 }
    203 
    204 void CommonCompilerTest::SetUpRuntimeOptions(RuntimeOptions* options) {
    205   CommonRuntimeTest::SetUpRuntimeOptions(options);
    206 
    207   compiler_options_.reset(new CompilerOptions);
    208   verification_results_.reset(new VerificationResults(compiler_options_.get()));
    209   callbacks_.reset(new QuickCompilerCallbacks(verification_results_.get(),
    210                                               CompilerCallbacks::CallbackMode::kCompileApp));
    211 }
    212 
    213 Compiler::Kind CommonCompilerTest::GetCompilerKind() const {
    214   return compiler_kind_;
    215 }
    216 
    217 void CommonCompilerTest::SetCompilerKind(Compiler::Kind compiler_kind) {
    218   compiler_kind_ = compiler_kind;
    219 }
    220 
    221 InstructionSet CommonCompilerTest::GetInstructionSet() const {
    222   DCHECK(compiler_driver_.get() != nullptr);
    223   return compiler_driver_->GetInstructionSet();
    224 }
    225 
    226 void CommonCompilerTest::TearDown() {
    227   timer_.reset();
    228   compiler_driver_.reset();
    229   callbacks_.reset();
    230   verification_results_.reset();
    231   compiler_options_.reset();
    232   image_reservation_.reset();
    233 
    234   CommonRuntimeTest::TearDown();
    235 }
    236 
    237 void CommonCompilerTest::CompileClass(mirror::ClassLoader* class_loader, const char* class_name) {
    238   std::string class_descriptor(DotToDescriptor(class_name));
    239   Thread* self = Thread::Current();
    240   StackHandleScope<1> hs(self);
    241   Handle<mirror::ClassLoader> loader(hs.NewHandle(class_loader));
    242   mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), loader);
    243   CHECK(klass != nullptr) << "Class not found " << class_name;
    244   auto pointer_size = class_linker_->GetImagePointerSize();
    245   for (auto& m : klass->GetMethods(pointer_size)) {
    246     CompileMethod(&m);
    247   }
    248 }
    249 
    250 void CommonCompilerTest::CompileMethod(ArtMethod* method) {
    251   CHECK(method != nullptr);
    252   TimingLogger timings("CommonTest::CompileMethod", false, false);
    253   TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
    254   compiler_driver_->CompileOne(Thread::Current(), method, &timings);
    255   TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
    256   MakeExecutable(method);
    257 }
    258 
    259 void CommonCompilerTest::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
    260                                              const char* class_name, const char* method_name,
    261                                              const char* signature) {
    262   std::string class_descriptor(DotToDescriptor(class_name));
    263   Thread* self = Thread::Current();
    264   mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
    265   CHECK(klass != nullptr) << "Class not found " << class_name;
    266   auto pointer_size = class_linker_->GetImagePointerSize();
    267   ArtMethod* method = klass->FindDirectMethod(method_name, signature, pointer_size);
    268   CHECK(method != nullptr) << "Direct method not found: "
    269       << class_name << "." << method_name << signature;
    270   CompileMethod(method);
    271 }
    272 
    273 void CommonCompilerTest::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
    274                                               const char* class_name, const char* method_name,
    275                                               const char* signature) {
    276   std::string class_descriptor(DotToDescriptor(class_name));
    277   Thread* self = Thread::Current();
    278   mirror::Class* klass = class_linker_->FindClass(self, class_descriptor.c_str(), class_loader);
    279   CHECK(klass != nullptr) << "Class not found " << class_name;
    280   auto pointer_size = class_linker_->GetImagePointerSize();
    281   ArtMethod* method = klass->FindVirtualMethod(method_name, signature, pointer_size);
    282   CHECK(method != nullptr) << "Virtual method not found: "
    283       << class_name << "." << method_name << signature;
    284   CompileMethod(method);
    285 }
    286 
    287 void CommonCompilerTest::ReserveImageSpace() {
    288   // Reserve where the image will be loaded up front so that other parts of test set up don't
    289   // accidentally end up colliding with the fixed memory address when we need to load the image.
    290   std::string error_msg;
    291   MemMap::Init();
    292   image_reservation_.reset(MemMap::MapAnonymous("image reservation",
    293                                                 reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
    294                                                 (size_t)120 * 1024 * 1024,  // 120MB
    295                                                 PROT_NONE,
    296                                                 false /* no need for 4gb flag with fixed mmap*/,
    297                                                 false /* not reusing existing reservation */,
    298                                                 &error_msg));
    299   CHECK(image_reservation_.get() != nullptr) << error_msg;
    300 }
    301 
    302 void CommonCompilerTest::UnreserveImageSpace() {
    303   image_reservation_.reset();
    304 }
    305 
    306 }  // namespace art
    307