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