Home | History | Annotate | Download | only in driver
      1 /*
      2  * Copyright (C) 2015 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 <gtest/gtest.h>
     18 
     19 #include "compiled_method_storage.h"
     20 #include "compiled_method.h"
     21 #include "compiler_driver.h"
     22 #include "compiler_options.h"
     23 #include "dex/verification_results.h"
     24 
     25 namespace art {
     26 
     27 TEST(CompiledMethodStorage, Deduplicate) {
     28   CompilerOptions compiler_options;
     29   VerificationResults verification_results(&compiler_options);
     30   CompilerDriver driver(&compiler_options,
     31                         &verification_results,
     32                         Compiler::kOptimizing,
     33                         /* instruction_set_ */ kNone,
     34                         /* instruction_set_features */ nullptr,
     35                         /* image_classes */ nullptr,
     36                         /* compiled_classes */ nullptr,
     37                         /* compiled_methods */ nullptr,
     38                         /* thread_count */ 1u,
     39                         /* dump_stats */ false,
     40                         /* dump_passes */ false,
     41                         /* timer */ nullptr,
     42                         /* swap_fd */ -1,
     43                         /* profile_compilation_info */ nullptr);
     44   CompiledMethodStorage* storage = driver.GetCompiledMethodStorage();
     45 
     46   ASSERT_TRUE(storage->DedupeEnabled());  // The default.
     47 
     48   const uint8_t raw_code1[] = { 1u, 2u, 3u };
     49   const uint8_t raw_code2[] = { 4u, 3u, 2u, 1u };
     50   ArrayRef<const uint8_t> code[] = {
     51       ArrayRef<const uint8_t>(raw_code1),
     52       ArrayRef<const uint8_t>(raw_code2),
     53   };
     54   const uint8_t raw_method_info_map1[] = { 1u, 2u, 3u, 4u, 5u, 6u };
     55   const uint8_t raw_method_info_map2[] = { 8u, 7u, 6u, 5u, 4u, 3u, 2u, 1u };
     56   ArrayRef<const uint8_t> method_info[] = {
     57       ArrayRef<const uint8_t>(raw_method_info_map1),
     58       ArrayRef<const uint8_t>(raw_method_info_map2),
     59   };
     60   const uint8_t raw_vmap_table1[] = { 2, 4, 6 };
     61   const uint8_t raw_vmap_table2[] = { 7, 5, 3, 1 };
     62   ArrayRef<const uint8_t> vmap_table[] = {
     63       ArrayRef<const uint8_t>(raw_vmap_table1),
     64       ArrayRef<const uint8_t>(raw_vmap_table2),
     65   };
     66   const uint8_t raw_cfi_info1[] = { 1, 3, 5 };
     67   const uint8_t raw_cfi_info2[] = { 8, 6, 4, 2 };
     68   ArrayRef<const uint8_t> cfi_info[] = {
     69       ArrayRef<const uint8_t>(raw_cfi_info1),
     70       ArrayRef<const uint8_t>(raw_cfi_info2),
     71   };
     72   const LinkerPatch raw_patches1[] = {
     73       LinkerPatch::CodePatch(0u, nullptr, 1u),
     74       LinkerPatch::MethodPatch(4u, nullptr, 1u),
     75   };
     76   const LinkerPatch raw_patches2[] = {
     77       LinkerPatch::CodePatch(0u, nullptr, 1u),
     78       LinkerPatch::MethodPatch(4u, nullptr, 2u),
     79   };
     80   ArrayRef<const LinkerPatch> patches[] = {
     81       ArrayRef<const LinkerPatch>(raw_patches1),
     82       ArrayRef<const LinkerPatch>(raw_patches2),
     83   };
     84 
     85   std::vector<CompiledMethod*> compiled_methods;
     86   compiled_methods.reserve(1u << 7);
     87   for (auto&& c : code) {
     88     for (auto&& s : method_info) {
     89       for (auto&& v : vmap_table) {
     90         for (auto&& f : cfi_info) {
     91           for (auto&& p : patches) {
     92             compiled_methods.push_back(CompiledMethod::SwapAllocCompiledMethod(
     93                 &driver, kNone, c, 0u, 0u, 0u, s, v, f, p));
     94           }
     95         }
     96       }
     97     }
     98   }
     99   constexpr size_t code_bit = 1u << 4;
    100   constexpr size_t src_map_bit = 1u << 3;
    101   constexpr size_t vmap_table_bit = 1u << 2;
    102   constexpr size_t cfi_info_bit = 1u << 1;
    103   constexpr size_t patches_bit = 1u << 0;
    104   CHECK_EQ(compiled_methods.size(), 1u << 5);
    105   for (size_t i = 0; i != compiled_methods.size(); ++i) {
    106     for (size_t j = 0; j != compiled_methods.size(); ++j) {
    107       CompiledMethod* lhs = compiled_methods[i];
    108       CompiledMethod* rhs = compiled_methods[j];
    109       bool same_code = ((i ^ j) & code_bit) == 0u;
    110       bool same_src_map = ((i ^ j) & src_map_bit) == 0u;
    111       bool same_vmap_table = ((i ^ j) & vmap_table_bit) == 0u;
    112       bool same_cfi_info = ((i ^ j) & cfi_info_bit) == 0u;
    113       bool same_patches = ((i ^ j) & patches_bit) == 0u;
    114       ASSERT_EQ(same_code, lhs->GetQuickCode().data() == rhs->GetQuickCode().data())
    115           << i << " " << j;
    116       ASSERT_EQ(same_src_map, lhs->GetMethodInfo().data() == rhs->GetMethodInfo().data())
    117           << i << " " << j;
    118       ASSERT_EQ(same_vmap_table, lhs->GetVmapTable().data() == rhs->GetVmapTable().data())
    119           << i << " " << j;
    120       ASSERT_EQ(same_cfi_info, lhs->GetCFIInfo().data() == rhs->GetCFIInfo().data())
    121           << i << " " << j;
    122       ASSERT_EQ(same_patches, lhs->GetPatches().data() == rhs->GetPatches().data())
    123           << i << " " << j;
    124     }
    125   }
    126   for (CompiledMethod* method : compiled_methods) {
    127     CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&driver, method);
    128   }
    129 }
    130 
    131 }  // namespace art
    132