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_64/relative_patcher_x86_64.h"
19 
20 namespace art {
21 namespace linker {
22 
23 class X86_64RelativePatcherTest : public RelativePatcherTest {
24  public:
X86_64RelativePatcherTest()25   X86_64RelativePatcherTest() : RelativePatcherTest(kX86_64, "default") { }
26 
27  protected:
28   static const uint8_t kCallRawCode[];
29   static const ArrayRef<const uint8_t> kCallCode;
30   static const uint8_t kDexCacheLoadRawCode[];
31   static const ArrayRef<const uint8_t> kDexCacheLoadCode;
32 
GetMethodOffset(uint32_t method_idx)33   uint32_t GetMethodOffset(uint32_t method_idx) {
34     auto result = method_offset_map_.FindMethodOffset(MethodRef(method_idx));
35     CHECK(result.first);
36     return result.second;
37   }
38 };
39 
40 const uint8_t X86_64RelativePatcherTest::kCallRawCode[] = {
41     0xe8, 0x00, 0x01, 0x00, 0x00
42 };
43 
44 const ArrayRef<const uint8_t> X86_64RelativePatcherTest::kCallCode(kCallRawCode);
45 
46 const uint8_t X86_64RelativePatcherTest::kDexCacheLoadRawCode[] = {
47     0x8b, 0x05,  // mov eax, [rip + <offset>]
48     0x00, 0x01, 0x00, 0x00
49 };
50 
51 const ArrayRef<const uint8_t> X86_64RelativePatcherTest::kDexCacheLoadCode(
52     kDexCacheLoadRawCode);
53 
TEST_F(X86_64RelativePatcherTest,CallSelf)54 TEST_F(X86_64RelativePatcherTest, CallSelf) {
55   LinkerPatch patches[] = {
56       LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 1u),
57   };
58   AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(patches));
59   Link();
60 
61   static const uint8_t expected_code[] = {
62       0xe8, 0xfb, 0xff, 0xff, 0xff
63   };
64   EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
65 }
66 
TEST_F(X86_64RelativePatcherTest,CallOther)67 TEST_F(X86_64RelativePatcherTest, CallOther) {
68   LinkerPatch method1_patches[] = {
69       LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 2u),
70   };
71   AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(method1_patches));
72   LinkerPatch method2_patches[] = {
73       LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 1u),
74   };
75   AddCompiledMethod(MethodRef(2u), kCallCode, ArrayRef<const LinkerPatch>(method2_patches));
76   Link();
77 
78   uint32_t method1_offset = GetMethodOffset(1u);
79   uint32_t method2_offset = GetMethodOffset(2u);
80   uint32_t diff_after = method2_offset - (method1_offset + kCallCode.size() /* PC adjustment */);
81   static const uint8_t method1_expected_code[] = {
82       0xe8,
83       static_cast<uint8_t>(diff_after), static_cast<uint8_t>(diff_after >> 8),
84       static_cast<uint8_t>(diff_after >> 16), static_cast<uint8_t>(diff_after >> 24)
85   };
86   EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(method1_expected_code)));
87   uint32_t diff_before = method1_offset - (method2_offset + kCallCode.size() /* PC adjustment */);
88   static const uint8_t method2_expected_code[] = {
89       0xe8,
90       static_cast<uint8_t>(diff_before), static_cast<uint8_t>(diff_before >> 8),
91       static_cast<uint8_t>(diff_before >> 16), static_cast<uint8_t>(diff_before >> 24)
92   };
93   EXPECT_TRUE(CheckLinkedMethod(MethodRef(2u), ArrayRef<const uint8_t>(method2_expected_code)));
94 }
95 
TEST_F(X86_64RelativePatcherTest,CallTrampoline)96 TEST_F(X86_64RelativePatcherTest, CallTrampoline) {
97   LinkerPatch patches[] = {
98       LinkerPatch::RelativeCodePatch(kCallCode.size() - 4u, nullptr, 2u),
99   };
100   AddCompiledMethod(MethodRef(1u), kCallCode, ArrayRef<const LinkerPatch>(patches));
101   Link();
102 
103   auto result = method_offset_map_.FindMethodOffset(MethodRef(1u));
104   ASSERT_TRUE(result.first);
105   uint32_t diff = kTrampolineOffset - (result.second + kCallCode.size());
106   static const uint8_t expected_code[] = {
107       0xe8,
108       static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8),
109       static_cast<uint8_t>(diff >> 16), static_cast<uint8_t>(diff >> 24)
110   };
111   EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
112 }
113 
TEST_F(X86_64RelativePatcherTest,DexCacheReference)114 TEST_F(X86_64RelativePatcherTest, DexCacheReference) {
115   dex_cache_arrays_begin_ = 0x12345678;
116   constexpr size_t kElementOffset = 0x1234;
117   LinkerPatch patches[] = {
118       LinkerPatch::DexCacheArrayPatch(kDexCacheLoadCode.size() - 4u, nullptr, 0u, kElementOffset),
119   };
120   AddCompiledMethod(MethodRef(1u), kDexCacheLoadCode, ArrayRef<const LinkerPatch>(patches));
121   Link();
122 
123   auto result = method_offset_map_.FindMethodOffset(MethodRef(1u));
124   ASSERT_TRUE(result.first);
125   uint32_t diff =
126       dex_cache_arrays_begin_ + kElementOffset - (result.second + kDexCacheLoadCode.size());
127   static const uint8_t expected_code[] = {
128       0x8b, 0x05,
129       static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8),
130       static_cast<uint8_t>(diff >> 16), static_cast<uint8_t>(diff >> 24)
131   };
132   EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
133 }
134 
135 }  // namespace linker
136 }  // namespace art
137