blob: 2a44b7990e8b5da9cca040a3b3b6ff9a80d339a0 [file] [log] [blame]
Vladimir Markob163bb72015-03-31 21:49:49 +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
17#include "linker/relative_patcher_test.h"
18#include "linker/x86/relative_patcher_x86.h"
19
20namespace art {
21namespace linker {
22
23class 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 Marko4d23c9d2015-04-01 23:03:09 +010030
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 Markob163bb72015-03-31 21:49:49 +010036};
37
38const uint8_t X86RelativePatcherTest::kCallRawCode[] = {
39 0xe8, 0x00, 0x01, 0x00, 0x00
40};
41
42const ArrayRef<const uint8_t> X86RelativePatcherTest::kCallCode(kCallRawCode);
43
44TEST_F(X86RelativePatcherTest, CallSelf) {
45 LinkerPatch patches[] = {
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010046 LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 1u),
Vladimir Markob163bb72015-03-31 21:49:49 +010047 };
Vladimir Markob207e142015-04-02 21:25:21 +010048 AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(patches));
Vladimir Markob163bb72015-03-31 21:49:49 +010049 Link();
50
51 static const uint8_t expected_code[] = {
52 0xe8, 0xfb, 0xff, 0xff, 0xff
53 };
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010054 EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
Vladimir Markob163bb72015-03-31 21:49:49 +010055}
56
57TEST_F(X86RelativePatcherTest, CallOther) {
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010058 LinkerPatch method1_patches[] = {
59 LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 2u),
60 };
Vladimir Markob207e142015-04-02 21:25:21 +010061 AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(method1_patches));
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010062 LinkerPatch method2_patches[] = {
Vladimir Markob163bb72015-03-31 21:49:49 +010063 LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 1u),
64 };
Vladimir Markob207e142015-04-02 21:25:21 +010065 AddCompiledMethod(MethodRef(2u), kCallCode, ArrayRef<const LinkerPatch>(method2_patches));
Vladimir Markob163bb72015-03-31 21:49:49 +010066 Link();
67
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010068 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 Markob163bb72015-03-31 21:49:49 +010072 0xe8,
Vladimir Markocac5a7e2016-02-22 10:39:50 +000073 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 Markob163bb72015-03-31 21:49:49 +010077 };
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010078 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 Markocac5a7e2016-02-22 10:39:50 +000082 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 Marko4d23c9d2015-04-01 23:03:09 +010086 };
87 EXPECT_TRUE(CheckLinkedMethod(MethodRef(2u), ArrayRef<const uint8_t>(method2_expected_code)));
Vladimir Markob163bb72015-03-31 21:49:49 +010088}
89
90TEST_F(X86RelativePatcherTest, CallTrampoline) {
91 LinkerPatch patches[] = {
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010092 LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 2u),
Vladimir Markob163bb72015-03-31 21:49:49 +010093 };
Vladimir Markob207e142015-04-02 21:25:21 +010094 AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(patches));
Vladimir Markob163bb72015-03-31 21:49:49 +010095 Link();
96
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010097 auto result = method_offset_map_.FindMethodOffset(MethodRef(1));
Vladimir Markob163bb72015-03-31 21:49:49 +010098 ASSERT_TRUE(result.first);
99 uint32_t diff = kTrampolineOffset - (result.second + kCallCode.size());
100 static const uint8_t expected_code[] = {
101 0xe8,
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000102 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 Markob163bb72015-03-31 21:49:49 +0100106 };
Vladimir Marko4d23c9d2015-04-01 23:03:09 +0100107 EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
Vladimir Markob163bb72015-03-31 21:49:49 +0100108}
109
Vladimir Marko1961b602015-04-08 20:51:48 +0100110TEST_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 Markocac5a7e2016-02-22 10:39:50 +0000134 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
142TEST_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 Marko1961b602015-04-08 20:51:48 +0100170 };
171 EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
172}
173
Vladimir Markob163bb72015-03-31 21:49:49 +0100174} // namespace linker
175} // namespace art