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:
X86RelativePatcherTest()25 X86RelativePatcherTest() : RelativePatcherTest(kX86, "default") { }
26
27 protected:
28 static const uint8_t kCallRawCode[];
29 static const ArrayRef<const uint8_t> kCallCode;
30
GetMethodOffset(uint32_t method_idx)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 }
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
TEST_F(X86RelativePatcherTest,CallSelf)44 TEST_F(X86RelativePatcherTest, CallSelf) {
45 LinkerPatch patches[] = {
46 LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 1u),
47 };
48 AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(patches));
49 Link();
50
51 static const uint8_t expected_code[] = {
52 0xe8, 0xfb, 0xff, 0xff, 0xff
53 };
54 EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
55 }
56
TEST_F(X86RelativePatcherTest,CallOther)57 TEST_F(X86RelativePatcherTest, CallOther) {
58 LinkerPatch method1_patches[] = {
59 LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 2u),
60 };
61 AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(method1_patches));
62 LinkerPatch method2_patches[] = {
63 LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 1u),
64 };
65 AddCompiledMethod(MethodRef(2u), kCallCode, ArrayRef<const LinkerPatch>(method2_patches));
66 Link();
67
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[] = {
72 0xe8,
73 static_cast<uint8_t>(diff_after), static_cast<uint8_t>(diff_after >> 8),
74 static_cast<uint8_t>(diff_after >> 16), static_cast<uint8_t>(diff_after >> 24)
75 };
76 EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(method1_expected_code)));
77 uint32_t diff_before = method1_offset - (method2_offset + kCallCode.size() /* PC adjustment */);
78 static const uint8_t method2_expected_code[] = {
79 0xe8,
80 static_cast<uint8_t>(diff_before), static_cast<uint8_t>(diff_before >> 8),
81 static_cast<uint8_t>(diff_before >> 16), static_cast<uint8_t>(diff_before >> 24)
82 };
83 EXPECT_TRUE(CheckLinkedMethod(MethodRef(2u), ArrayRef<const uint8_t>(method2_expected_code)));
84 }
85
TEST_F(X86RelativePatcherTest,CallTrampoline)86 TEST_F(X86RelativePatcherTest, CallTrampoline) {
87 LinkerPatch patches[] = {
88 LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 2u),
89 };
90 AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(patches));
91 Link();
92
93 auto result = method_offset_map_.FindMethodOffset(MethodRef(1));
94 ASSERT_TRUE(result.first);
95 uint32_t diff = kTrampolineOffset - (result.second + kCallCode.size());
96 static const uint8_t expected_code[] = {
97 0xe8,
98 static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8),
99 static_cast<uint8_t>(diff >> 16), static_cast<uint8_t>(diff >> 24)
100 };
101 EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
102 }
103
TEST_F(X86RelativePatcherTest,DexCacheReference)104 TEST_F(X86RelativePatcherTest, DexCacheReference) {
105 dex_cache_arrays_begin_ = 0x12345678;
106 constexpr size_t kElementOffset = 0x1234;
107 static const uint8_t raw_code[] = {
108 0xe8, 0x00, 0x00, 0x00, 0x00, // call +0
109 0x5b, // pop ebx
110 0x8b, 0x83, 0x00, 0x01, 0x00, 0x00, // mov eax, [ebx + 256 (kDummy32BitValue)]
111 };
112 constexpr uint32_t anchor_offset = 5u; // After call +0.
113 ArrayRef<const uint8_t> code(raw_code);
114 LinkerPatch patches[] = {
115 LinkerPatch::DexCacheArrayPatch(code.size() - 4u, nullptr, anchor_offset, kElementOffset),
116 };
117 AddCompiledMethod(MethodRef(1u), code, ArrayRef<const LinkerPatch>(patches));
118 Link();
119
120 auto result = method_offset_map_.FindMethodOffset(MethodRef(1u));
121 ASSERT_TRUE(result.first);
122 uint32_t diff =
123 dex_cache_arrays_begin_ + kElementOffset - (result.second + anchor_offset);
124 static const uint8_t expected_code[] = {
125 0xe8, 0x00, 0x00, 0x00, 0x00, // call +0
126 0x5b, // pop ebx
127 0x8b, 0x83, // mov eax, [ebx + diff]
128 static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8),
129 static_cast<uint8_t>(diff >> 16), static_cast<uint8_t>(diff >> 24)
130 };
131 EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
132 }
133
134 } // namespace linker
135 } // namespace art
136