Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +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 | |
| 17 | #include "linker/relative_patcher_test.h" |
| 18 | #include "linker/x86/relative_patcher_x86.h" |
| 19 | |
| 20 | namespace art { |
| 21 | namespace linker { |
| 22 | |
| 23 | class X86RelativePatcherTest : public RelativePatcherTest { |
| 24 | public: |
| 25 | X86RelativePatcherTest() : RelativePatcherTest(kX86, "default") { } |
| 26 | |
| 27 | protected: |
| 28 | static const uint8_t kCallRawCode[]; |
| 29 | static const ArrayRef<const uint8_t> kCallCode; |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 30 | |
| 31 | uint32_t GetMethodOffset(uint32_t method_idx) { |
| 32 | auto result = method_offset_map_.FindMethodOffset(MethodRef(method_idx)); |
| 33 | CHECK(result.first); |
| 34 | return result.second; |
| 35 | } |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | const uint8_t X86RelativePatcherTest::kCallRawCode[] = { |
| 39 | 0xe8, 0x00, 0x01, 0x00, 0x00 |
| 40 | }; |
| 41 | |
| 42 | const ArrayRef<const uint8_t> X86RelativePatcherTest::kCallCode(kCallRawCode); |
| 43 | |
| 44 | TEST_F(X86RelativePatcherTest, CallSelf) { |
| 45 | LinkerPatch patches[] = { |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 46 | LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 1u), |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 47 | }; |
Vladimir Marko | b207e14 | 2015-04-02 21:25:21 +0100 | [diff] [blame] | 48 | AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(patches)); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 49 | Link(); |
| 50 | |
| 51 | static const uint8_t expected_code[] = { |
| 52 | 0xe8, 0xfb, 0xff, 0xff, 0xff |
| 53 | }; |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 54 | EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code))); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | TEST_F(X86RelativePatcherTest, CallOther) { |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 58 | LinkerPatch method1_patches[] = { |
| 59 | LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 2u), |
| 60 | }; |
Vladimir Marko | b207e14 | 2015-04-02 21:25:21 +0100 | [diff] [blame] | 61 | AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(method1_patches)); |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 62 | LinkerPatch method2_patches[] = { |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 63 | LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 1u), |
| 64 | }; |
Vladimir Marko | b207e14 | 2015-04-02 21:25:21 +0100 | [diff] [blame] | 65 | AddCompiledMethod(MethodRef(2u), kCallCode, ArrayRef<const LinkerPatch>(method2_patches)); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 66 | Link(); |
| 67 | |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 68 | uint32_t method1_offset = GetMethodOffset(1u); |
| 69 | uint32_t method2_offset = GetMethodOffset(2u); |
| 70 | uint32_t diff_after = method2_offset - (method1_offset + kCallCode.size() /* PC adjustment */); |
| 71 | static const uint8_t method1_expected_code[] = { |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 72 | 0xe8, |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 73 | static_cast<uint8_t>(diff_after), |
| 74 | static_cast<uint8_t>(diff_after >> 8), |
| 75 | static_cast<uint8_t>(diff_after >> 16), |
| 76 | static_cast<uint8_t>(diff_after >> 24) |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 77 | }; |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 78 | EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(method1_expected_code))); |
| 79 | uint32_t diff_before = method1_offset - (method2_offset + kCallCode.size() /* PC adjustment */); |
| 80 | static const uint8_t method2_expected_code[] = { |
| 81 | 0xe8, |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 82 | static_cast<uint8_t>(diff_before), |
| 83 | static_cast<uint8_t>(diff_before >> 8), |
| 84 | static_cast<uint8_t>(diff_before >> 16), |
| 85 | static_cast<uint8_t>(diff_before >> 24) |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 86 | }; |
| 87 | EXPECT_TRUE(CheckLinkedMethod(MethodRef(2u), ArrayRef<const uint8_t>(method2_expected_code))); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST_F(X86RelativePatcherTest, CallTrampoline) { |
| 91 | LinkerPatch patches[] = { |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 92 | LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 2u), |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 93 | }; |
Vladimir Marko | b207e14 | 2015-04-02 21:25:21 +0100 | [diff] [blame] | 94 | AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(patches)); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 95 | Link(); |
| 96 | |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 97 | auto result = method_offset_map_.FindMethodOffset(MethodRef(1)); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 98 | ASSERT_TRUE(result.first); |
| 99 | uint32_t diff = kTrampolineOffset - (result.second + kCallCode.size()); |
| 100 | static const uint8_t expected_code[] = { |
| 101 | 0xe8, |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 102 | static_cast<uint8_t>(diff), |
| 103 | static_cast<uint8_t>(diff >> 8), |
| 104 | static_cast<uint8_t>(diff >> 16), |
| 105 | static_cast<uint8_t>(diff >> 24) |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 106 | }; |
Vladimir Marko | 4d23c9d | 2015-04-01 23:03:09 +0100 | [diff] [blame] | 107 | EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code))); |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Vladimir Marko | 1961b60 | 2015-04-08 20:51:48 +0100 | [diff] [blame] | 110 | TEST_F(X86RelativePatcherTest, DexCacheReference) { |
| 111 | dex_cache_arrays_begin_ = 0x12345678; |
| 112 | constexpr size_t kElementOffset = 0x1234; |
| 113 | static const uint8_t raw_code[] = { |
| 114 | 0xe8, 0x00, 0x00, 0x00, 0x00, // call +0 |
| 115 | 0x5b, // pop ebx |
| 116 | 0x8b, 0x83, 0x00, 0x01, 0x00, 0x00, // mov eax, [ebx + 256 (kDummy32BitValue)] |
| 117 | }; |
| 118 | constexpr uint32_t anchor_offset = 5u; // After call +0. |
| 119 | ArrayRef<const uint8_t> code(raw_code); |
| 120 | LinkerPatch patches[] = { |
| 121 | LinkerPatch::DexCacheArrayPatch(code.size() - 4u, nullptr, anchor_offset, kElementOffset), |
| 122 | }; |
| 123 | AddCompiledMethod(MethodRef(1u), code, ArrayRef<const LinkerPatch>(patches)); |
| 124 | Link(); |
| 125 | |
| 126 | auto result = method_offset_map_.FindMethodOffset(MethodRef(1u)); |
| 127 | ASSERT_TRUE(result.first); |
| 128 | uint32_t diff = |
| 129 | dex_cache_arrays_begin_ + kElementOffset - (result.second + anchor_offset); |
| 130 | static const uint8_t expected_code[] = { |
| 131 | 0xe8, 0x00, 0x00, 0x00, 0x00, // call +0 |
| 132 | 0x5b, // pop ebx |
| 133 | 0x8b, 0x83, // mov eax, [ebx + diff] |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 134 | static_cast<uint8_t>(diff), |
| 135 | static_cast<uint8_t>(diff >> 8), |
| 136 | static_cast<uint8_t>(diff >> 16), |
| 137 | static_cast<uint8_t>(diff >> 24) |
| 138 | }; |
| 139 | EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code))); |
| 140 | } |
| 141 | |
| 142 | TEST_F(X86RelativePatcherTest, StringReference) { |
| 143 | constexpr uint32_t kStringIndex = 1u; |
| 144 | constexpr uint32_t kStringOffset = 0x12345678; |
| 145 | string_index_to_offset_map_.Put(kStringIndex, kStringOffset); |
| 146 | static const uint8_t raw_code[] = { |
| 147 | 0xe8, 0x00, 0x00, 0x00, 0x00, // call +0 |
| 148 | 0x5b, // pop ebx |
| 149 | 0x8d, 0x83, 0x00, 0x01, 0x00, 0x00, // lea eax, [ebx + 256 (kDummy32BitValue)] |
| 150 | }; |
| 151 | constexpr uint32_t anchor_offset = 5u; // After call +0. |
| 152 | ArrayRef<const uint8_t> code(raw_code); |
| 153 | LinkerPatch patches[] = { |
| 154 | LinkerPatch::RelativeStringPatch(code.size() - 4u, nullptr, anchor_offset, kStringIndex), |
| 155 | }; |
| 156 | AddCompiledMethod(MethodRef(1u), code, ArrayRef<const LinkerPatch>(patches)); |
| 157 | Link(); |
| 158 | |
| 159 | auto result = method_offset_map_.FindMethodOffset(MethodRef(1u)); |
| 160 | ASSERT_TRUE(result.first); |
| 161 | uint32_t diff = kStringOffset - (result.second + anchor_offset); |
| 162 | static const uint8_t expected_code[] = { |
| 163 | 0xe8, 0x00, 0x00, 0x00, 0x00, // call +0 |
| 164 | 0x5b, // pop ebx |
| 165 | 0x8d, 0x83, // lea eax, [ebx + diff] |
| 166 | static_cast<uint8_t>(diff), |
| 167 | static_cast<uint8_t>(diff >> 8), |
| 168 | static_cast<uint8_t>(diff >> 16), |
| 169 | static_cast<uint8_t>(diff >> 24) |
Vladimir Marko | 1961b60 | 2015-04-08 20:51:48 +0100 | [diff] [blame] | 170 | }; |
| 171 | EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code))); |
| 172 | } |
| 173 | |
Vladimir Marko | b163bb7 | 2015-03-31 21:49:49 +0100 | [diff] [blame] | 174 | } // namespace linker |
| 175 | } // namespace art |