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