| 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 | #include "compiler_driver.h" |
| 23 | #include "compiler_options.h" |
| 24 | #include "dex/verification_results.h" |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | TEST(CompiledMethodStorage, Deduplicate) { |
| 29 | CompilerOptions compiler_options; |
| 30 | VerificationResults verification_results(&compiler_options); |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 31 | CompilerDriver driver(&compiler_options, |
| 32 | &verification_results, |
| Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 33 | Compiler::kOptimizing, |
| Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 34 | /* image_classes */ nullptr, |
| Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 35 | /* thread_count */ 1u, |
| Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 36 | /* swap_fd */ -1, |
| 37 | /* profile_compilation_info */ nullptr); |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 38 | CompiledMethodStorage* storage = driver.GetCompiledMethodStorage(); |
| 39 | |
| 40 | ASSERT_TRUE(storage->DedupeEnabled()); // The default. |
| 41 | |
| 42 | const uint8_t raw_code1[] = { 1u, 2u, 3u }; |
| 43 | const uint8_t raw_code2[] = { 4u, 3u, 2u, 1u }; |
| 44 | ArrayRef<const uint8_t> code[] = { |
| 45 | ArrayRef<const uint8_t>(raw_code1), |
| 46 | ArrayRef<const uint8_t>(raw_code2), |
| 47 | }; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 48 | const uint8_t raw_vmap_table1[] = { 2, 4, 6 }; |
| 49 | const uint8_t raw_vmap_table2[] = { 7, 5, 3, 1 }; |
| 50 | ArrayRef<const uint8_t> vmap_table[] = { |
| 51 | ArrayRef<const uint8_t>(raw_vmap_table1), |
| 52 | ArrayRef<const uint8_t>(raw_vmap_table2), |
| 53 | }; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 54 | const uint8_t raw_cfi_info1[] = { 1, 3, 5 }; |
| 55 | const uint8_t raw_cfi_info2[] = { 8, 6, 4, 2 }; |
| 56 | ArrayRef<const uint8_t> cfi_info[] = { |
| 57 | ArrayRef<const uint8_t>(raw_cfi_info1), |
| 58 | ArrayRef<const uint8_t>(raw_cfi_info2), |
| 59 | }; |
| Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 60 | const linker::LinkerPatch raw_patches1[] = { |
| Vladimir Marko | 8e524ad | 2018-07-13 10:27:43 +0100 | [diff] [blame] | 61 | linker::LinkerPatch::IntrinsicReferencePatch(0u, 0u, 0u), |
| Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 62 | linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 1u), |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 63 | }; |
| Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 64 | const linker::LinkerPatch raw_patches2[] = { |
| Vladimir Marko | 8e524ad | 2018-07-13 10:27:43 +0100 | [diff] [blame] | 65 | linker::LinkerPatch::IntrinsicReferencePatch(0u, 0u, 0u), |
| Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 66 | linker::LinkerPatch::RelativeMethodPatch(4u, nullptr, 0u, 2u), |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 67 | }; |
| Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 68 | ArrayRef<const linker::LinkerPatch> patches[] = { |
| 69 | ArrayRef<const linker::LinkerPatch>(raw_patches1), |
| 70 | ArrayRef<const linker::LinkerPatch>(raw_patches2), |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | std::vector<CompiledMethod*> compiled_methods; |
| David Srbecky | 8cd5454 | 2018-07-15 23:58:44 +0100 | [diff] [blame^] | 74 | compiled_methods.reserve(1u << 4); |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 75 | for (auto&& c : code) { |
| David Srbecky | 8cd5454 | 2018-07-15 23:58:44 +0100 | [diff] [blame^] | 76 | for (auto&& v : vmap_table) { |
| 77 | for (auto&& f : cfi_info) { |
| 78 | for (auto&& p : patches) { |
| 79 | compiled_methods.push_back(CompiledMethod::SwapAllocCompiledMethod( |
| 80 | &driver, InstructionSet::kNone, c, v, f, p)); |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| David Srbecky | 8cd5454 | 2018-07-15 23:58:44 +0100 | [diff] [blame^] | 85 | constexpr size_t code_bit = 1u << 3; |
| Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 86 | constexpr size_t vmap_table_bit = 1u << 2; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 87 | constexpr size_t cfi_info_bit = 1u << 1; |
| 88 | constexpr size_t patches_bit = 1u << 0; |
| David Srbecky | 8cd5454 | 2018-07-15 23:58:44 +0100 | [diff] [blame^] | 89 | CHECK_EQ(compiled_methods.size(), 1u << 4); |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 90 | for (size_t i = 0; i != compiled_methods.size(); ++i) { |
| 91 | for (size_t j = 0; j != compiled_methods.size(); ++j) { |
| 92 | CompiledMethod* lhs = compiled_methods[i]; |
| 93 | CompiledMethod* rhs = compiled_methods[j]; |
| 94 | bool same_code = ((i ^ j) & code_bit) == 0u; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 95 | bool same_vmap_table = ((i ^ j) & vmap_table_bit) == 0u; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 96 | bool same_cfi_info = ((i ^ j) & cfi_info_bit) == 0u; |
| 97 | bool same_patches = ((i ^ j) & patches_bit) == 0u; |
| 98 | ASSERT_EQ(same_code, lhs->GetQuickCode().data() == rhs->GetQuickCode().data()) |
| 99 | << i << " " << j; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 100 | ASSERT_EQ(same_vmap_table, lhs->GetVmapTable().data() == rhs->GetVmapTable().data()) |
| 101 | << i << " " << j; |
| Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 102 | ASSERT_EQ(same_cfi_info, lhs->GetCFIInfo().data() == rhs->GetCFIInfo().data()) |
| 103 | << i << " " << j; |
| 104 | ASSERT_EQ(same_patches, lhs->GetPatches().data() == rhs->GetPatches().data()) |
| 105 | << i << " " << j; |
| 106 | } |
| 107 | } |
| 108 | for (CompiledMethod* method : compiled_methods) { |
| 109 | CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&driver, method); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | } // namespace art |