| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 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 | |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include "compiled_method_storage.h" |
| 18 | |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
| 20 | |
| Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 21 | #include "compiled_method-inl.h" |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | TEST(CompiledMethodStorage, Deduplicate) { |
| Andreas Gampe | 3db7068 | 2018-12-26 15:12:03 -0800 | [diff] [blame] | 26 | CompiledMethodStorage storage(/* swap_fd= */ -1); |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 27 | |
| Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 28 | ASSERT_TRUE(storage.DedupeEnabled()); // The default. |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 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 | }; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 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 | }; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 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 | }; |
| Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 48 | const linker::LinkerPatch raw_patches1[] = { |
| Vladimir Marko | 8e524ad | 2018-07-13 10:27:43 +0100 | [diff] [blame] | 49 | linker::LinkerPatch::IntrinsicReferencePatch(0u, 0u, 0u), |
| Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 50 | linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 1u), |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 51 | }; |
| Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 52 | const linker::LinkerPatch raw_patches2[] = { |
| Vladimir Marko | 8e524ad | 2018-07-13 10:27:43 +0100 | [diff] [blame] | 53 | linker::LinkerPatch::IntrinsicReferencePatch(0u, 0u, 0u), |
| Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 54 | linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 2u), |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 55 | }; |
| Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 56 | ArrayRef<const linker::LinkerPatch> patches[] = { |
| 57 | ArrayRef<const linker::LinkerPatch>(raw_patches1), |
| 58 | ArrayRef<const linker::LinkerPatch>(raw_patches2), |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | std::vector<CompiledMethod*> compiled_methods; |
| David Srbecky | 8cd5454 | 2018-07-15 23:58:44 +0100 | [diff] [blame] | 62 | compiled_methods.reserve(1u << 4); |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 63 | for (auto&& c : code) { |
| David Srbecky | 8cd5454 | 2018-07-15 23:58:44 +0100 | [diff] [blame] | 64 | for (auto&& v : vmap_table) { |
| 65 | for (auto&& f : cfi_info) { |
| 66 | for (auto&& p : patches) { |
| 67 | compiled_methods.push_back(CompiledMethod::SwapAllocCompiledMethod( |
| Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 68 | &storage, InstructionSet::kNone, c, v, f, p)); |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| David Srbecky | 8cd5454 | 2018-07-15 23:58:44 +0100 | [diff] [blame] | 73 | constexpr size_t code_bit = 1u << 3; |
| Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 74 | constexpr size_t vmap_table_bit = 1u << 2; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 75 | constexpr size_t cfi_info_bit = 1u << 1; |
| 76 | constexpr size_t patches_bit = 1u << 0; |
| David Srbecky | 8cd5454 | 2018-07-15 23:58:44 +0100 | [diff] [blame] | 77 | CHECK_EQ(compiled_methods.size(), 1u << 4); |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 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; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 83 | bool same_vmap_table = ((i ^ j) & vmap_table_bit) == 0u; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 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; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 88 | ASSERT_EQ(same_vmap_table, lhs->GetVmapTable().data() == rhs->GetVmapTable().data()) |
| 89 | << i << " " << j; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 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) { |
| Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 97 | CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&storage, method); |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | } // namespace art |