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