blob: 05eacd848d2665d77b3b50b24c77011681b2d522 [file] [log] [blame]
Vladimir Marko35831e82015-09-11 11:59:18 +01001/*
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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070017#include "compiled_method_storage.h"
18
Vladimir Marko35831e82015-09-11 11:59:18 +010019#include <gtest/gtest.h>
20
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010021#include "compiled_method-inl.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010022
23namespace art {
24
25TEST(CompiledMethodStorage, Deduplicate) {
Andreas Gampe3db70682018-12-26 15:12:03 -080026 CompiledMethodStorage storage(/* swap_fd= */ -1);
Vladimir Marko35831e82015-09-11 11:59:18 +010027
Vladimir Marko33f7c8a2018-11-19 10:22:01 +000028 ASSERT_TRUE(storage.DedupeEnabled()); // The default.
Vladimir Marko35831e82015-09-11 11:59:18 +010029
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 };
Vladimir Marko35831e82015-09-11 11:59:18 +010036 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 };
Vladimir Marko35831e82015-09-11 11:59:18 +010042 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 };
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010048 const linker::LinkerPatch raw_patches1[] = {
Vladimir Marko8e524ad2018-07-13 10:27:43 +010049 linker::LinkerPatch::IntrinsicReferencePatch(0u, 0u, 0u),
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010050 linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 1u),
Vladimir Marko35831e82015-09-11 11:59:18 +010051 };
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010052 const linker::LinkerPatch raw_patches2[] = {
Vladimir Marko8e524ad2018-07-13 10:27:43 +010053 linker::LinkerPatch::IntrinsicReferencePatch(0u, 0u, 0u),
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010054 linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 2u),
Vladimir Marko35831e82015-09-11 11:59:18 +010055 };
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010056 ArrayRef<const linker::LinkerPatch> patches[] = {
57 ArrayRef<const linker::LinkerPatch>(raw_patches1),
58 ArrayRef<const linker::LinkerPatch>(raw_patches2),
Vladimir Marko35831e82015-09-11 11:59:18 +010059 };
60
61 std::vector<CompiledMethod*> compiled_methods;
David Srbecky8cd54542018-07-15 23:58:44 +010062 compiled_methods.reserve(1u << 4);
Vladimir Marko35831e82015-09-11 11:59:18 +010063 for (auto&& c : code) {
David Srbecky8cd54542018-07-15 23:58:44 +010064 for (auto&& v : vmap_table) {
65 for (auto&& f : cfi_info) {
66 for (auto&& p : patches) {
67 compiled_methods.push_back(CompiledMethod::SwapAllocCompiledMethod(
Vladimir Marko33f7c8a2018-11-19 10:22:01 +000068 &storage, InstructionSet::kNone, c, v, f, p));
Vladimir Marko35831e82015-09-11 11:59:18 +010069 }
70 }
71 }
72 }
David Srbecky8cd54542018-07-15 23:58:44 +010073 constexpr size_t code_bit = 1u << 3;
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010074 constexpr size_t vmap_table_bit = 1u << 2;
Vladimir Marko35831e82015-09-11 11:59:18 +010075 constexpr size_t cfi_info_bit = 1u << 1;
76 constexpr size_t patches_bit = 1u << 0;
David Srbecky8cd54542018-07-15 23:58:44 +010077 CHECK_EQ(compiled_methods.size(), 1u << 4);
Vladimir Marko35831e82015-09-11 11:59:18 +010078 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;
Vladimir Marko35831e82015-09-11 11:59:18 +010083 bool same_vmap_table = ((i ^ j) & vmap_table_bit) == 0u;
Vladimir Marko35831e82015-09-11 11:59:18 +010084 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;
Vladimir Marko35831e82015-09-11 11:59:18 +010088 ASSERT_EQ(same_vmap_table, lhs->GetVmapTable().data() == rhs->GetVmapTable().data())
89 << i << " " << j;
Vladimir Marko35831e82015-09-11 11:59:18 +010090 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) {
Vladimir Marko33f7c8a2018-11-19 10:22:01 +000097 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&storage, method);
Vladimir Marko35831e82015-09-11 11:59:18 +010098 }
99}
100
101} // namespace art