1 /*
2  * Copyright (C) 2016 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/mips/relative_patcher_mips.h"
19 
20 namespace art {
21 namespace linker {
22 
23 class Mips32r6RelativePatcherTest : public RelativePatcherTest {
24  public:
Mips32r6RelativePatcherTest()25   Mips32r6RelativePatcherTest() : RelativePatcherTest(kMips, "mips32r6") {}
26 
27  protected:
28   static const uint8_t kUnpatchedPcRelativeRawCode[];
29   static const uint32_t kLiteralOffset;
30   static const uint32_t kAnchorOffset;
31   static const ArrayRef<const uint8_t> kUnpatchedPcRelativeCode;
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   void CheckPcRelativePatch(const ArrayRef<const LinkerPatch>& patches, uint32_t target_offset);
40   void TestDexCacheReference(uint32_t dex_cache_arrays_begin, uint32_t element_offset);
41   void TestStringReference(uint32_t string_offset);
42 };
43 
44 const uint8_t Mips32r6RelativePatcherTest::kUnpatchedPcRelativeRawCode[] = {
45     0x34, 0x12, 0x5E, 0xEE,  // auipc s2, high(diff); placeholder = 0x1234
46     0x78, 0x56, 0x52, 0x26,  // addiu s2, s2, low(diff); placeholder = 0x5678
47 };
48 const uint32_t Mips32r6RelativePatcherTest::kLiteralOffset = 0;  // At auipc (where
49                                                                  // patching starts).
50 const uint32_t Mips32r6RelativePatcherTest::kAnchorOffset = 0;  // At auipc (where PC+0 points).
51 const ArrayRef<const uint8_t> Mips32r6RelativePatcherTest::kUnpatchedPcRelativeCode(
52     kUnpatchedPcRelativeRawCode);
53 
CheckPcRelativePatch(const ArrayRef<const LinkerPatch> & patches,uint32_t target_offset)54 void Mips32r6RelativePatcherTest::CheckPcRelativePatch(const ArrayRef<const LinkerPatch>& patches,
55                                                        uint32_t target_offset) {
56   AddCompiledMethod(MethodRef(1u), kUnpatchedPcRelativeCode, ArrayRef<const LinkerPatch>(patches));
57   Link();
58 
59   auto result = method_offset_map_.FindMethodOffset(MethodRef(1u));
60   ASSERT_TRUE(result.first);
61 
62   uint32_t diff = target_offset - (result.second + kAnchorOffset);
63   diff += (diff & 0x8000) << 1;  // Account for sign extension in addiu.
64 
65   const uint8_t expected_code[] = {
66       static_cast<uint8_t>(diff >> 16), static_cast<uint8_t>(diff >> 24), 0x5E, 0xEE,
67       static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8), 0x52, 0x26,
68   };
69   EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
70 }
71 
TestDexCacheReference(uint32_t dex_cache_arrays_begin,uint32_t element_offset)72 void Mips32r6RelativePatcherTest::TestDexCacheReference(uint32_t dex_cache_arrays_begin,
73                                                         uint32_t element_offset) {
74   dex_cache_arrays_begin_ = dex_cache_arrays_begin;
75   LinkerPatch patches[] = {
76       LinkerPatch::DexCacheArrayPatch(kLiteralOffset, nullptr, kAnchorOffset, element_offset)
77   };
78   CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches),
79                        dex_cache_arrays_begin_ + element_offset);
80 }
81 
TestStringReference(uint32_t string_offset)82 void Mips32r6RelativePatcherTest::TestStringReference(uint32_t string_offset) {
83   constexpr uint32_t kStringIndex = 1u;
84   string_index_to_offset_map_.Put(kStringIndex, string_offset);
85   LinkerPatch patches[] = {
86       LinkerPatch::RelativeStringPatch(kLiteralOffset, nullptr, kAnchorOffset, kStringIndex)
87   };
88   CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches), string_offset);
89 }
90 
TEST_F(Mips32r6RelativePatcherTest,DexCacheReference)91 TEST_F(Mips32r6RelativePatcherTest, DexCacheReference) {
92   TestDexCacheReference(/* dex_cache_arrays_begin */ 0x12345678, /* element_offset */ 0x1234);
93 }
94 
TEST_F(Mips32r6RelativePatcherTest,StringReference)95 TEST_F(Mips32r6RelativePatcherTest, StringReference) {
96   TestStringReference(/* string_offset*/ 0x87651234);
97 }
98 
99 }  // namespace linker
100 }  // namespace art
101