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
23 namespace art {
24
TEST(CompiledMethodStorage,Deduplicate)25 TEST(CompiledMethodStorage, Deduplicate) {
26 CompiledMethodStorage storage(/* swap_fd= */ -1);
27
28 ASSERT_TRUE(storage.DedupeEnabled()); // The default.
29
30 const uint8_t raw_code1[] = { 1u, 2u, 3u };
31 const uint8_t raw_code2[] = { 4u, 3u, 2u, 1u };
32 ArrayRef<const uint8_t> code[] = {
33 ArrayRef<const uint8_t>(raw_code1),
34 ArrayRef<const uint8_t>(raw_code2),
35 };
36 const uint8_t raw_vmap_table1[] = { 2, 4, 6 };
37 const uint8_t raw_vmap_table2[] = { 7, 5, 3, 1 };
38 ArrayRef<const uint8_t> vmap_table[] = {
39 ArrayRef<const uint8_t>(raw_vmap_table1),
40 ArrayRef<const uint8_t>(raw_vmap_table2),
41 };
42 const uint8_t raw_cfi_info1[] = { 1, 3, 5 };
43 const uint8_t raw_cfi_info2[] = { 8, 6, 4, 2 };
44 ArrayRef<const uint8_t> cfi_info[] = {
45 ArrayRef<const uint8_t>(raw_cfi_info1),
46 ArrayRef<const uint8_t>(raw_cfi_info2),
47 };
48 const linker::LinkerPatch raw_patches1[] = {
49 linker::LinkerPatch::IntrinsicReferencePatch(0u, 0u, 0u),
50 linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 1u),
51 };
52 const linker::LinkerPatch raw_patches2[] = {
53 linker::LinkerPatch::IntrinsicReferencePatch(0u, 0u, 0u),
54 linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 2u),
55 };
56 ArrayRef<const linker::LinkerPatch> patches[] = {
57 ArrayRef<const linker::LinkerPatch>(raw_patches1),
58 ArrayRef<const linker::LinkerPatch>(raw_patches2),
59 };
60
61 std::vector<CompiledMethod*> compiled_methods;
62 compiled_methods.reserve(1u << 4);
63 for (auto&& c : code) {
64 for (auto&& v : vmap_table) {
65 for (auto&& f : cfi_info) {
66 for (auto&& p : patches) {
67 compiled_methods.push_back(CompiledMethod::SwapAllocCompiledMethod(
68 &storage, InstructionSet::kNone, c, v, f, p));
69 }
70 }
71 }
72 }
73 constexpr size_t code_bit = 1u << 3;
74 constexpr size_t vmap_table_bit = 1u << 2;
75 constexpr size_t cfi_info_bit = 1u << 1;
76 constexpr size_t patches_bit = 1u << 0;
77 CHECK_EQ(compiled_methods.size(), 1u << 4);
78 for (size_t i = 0; i != compiled_methods.size(); ++i) {
79 for (size_t j = 0; j != compiled_methods.size(); ++j) {
80 CompiledMethod* lhs = compiled_methods[i];
81 CompiledMethod* rhs = compiled_methods[j];
82 bool same_code = ((i ^ j) & code_bit) == 0u;
83 bool same_vmap_table = ((i ^ j) & vmap_table_bit) == 0u;
84 bool same_cfi_info = ((i ^ j) & cfi_info_bit) == 0u;
85 bool same_patches = ((i ^ j) & patches_bit) == 0u;
86 ASSERT_EQ(same_code, lhs->GetQuickCode().data() == rhs->GetQuickCode().data())
87 << i << " " << j;
88 ASSERT_EQ(same_vmap_table, lhs->GetVmapTable().data() == rhs->GetVmapTable().data())
89 << i << " " << j;
90 ASSERT_EQ(same_cfi_info, lhs->GetCFIInfo().data() == rhs->GetCFIInfo().data())
91 << i << " " << j;
92 ASSERT_EQ(same_patches, lhs->GetPatches().data() == rhs->GetPatches().data())
93 << i << " " << j;
94 }
95 }
96 for (CompiledMethod* method : compiled_methods) {
97 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&storage, method);
98 }
99 }
100
101 } // namespace art
102