• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // We'll maximize the range of a single load instruction for dex cache array accesses
24 // by aligning offset -32768 with the offset of the first used element.
25 static constexpr uint32_t kDexCacheArrayLwOffset = 0x8000;
26 
27 class MipsRelativePatcherTest : public RelativePatcherTest {
28  public:
MipsRelativePatcherTest()29   MipsRelativePatcherTest() : RelativePatcherTest(kMips, "mips32r2") {}
30 
31  protected:
32   static const uint8_t kUnpatchedPcRelativeRawCode[];
33   static const uint32_t kLiteralOffset;
34   static const uint32_t kAnchorOffset;
35   static const ArrayRef<const uint8_t> kUnpatchedPcRelativeCode;
36 
GetMethodOffset(uint32_t method_idx)37   uint32_t GetMethodOffset(uint32_t method_idx) {
38     auto result = method_offset_map_.FindMethodOffset(MethodRef(method_idx));
39     CHECK(result.first);
40     return result.second;
41   }
42 
43   void CheckPcRelativePatch(const ArrayRef<const LinkerPatch>& patches, uint32_t target_offset);
44   void TestDexCacheReference(uint32_t dex_cache_arrays_begin, uint32_t element_offset);
45   void TestStringReference(uint32_t string_offset);
46 };
47 
48 const uint8_t MipsRelativePatcherTest::kUnpatchedPcRelativeRawCode[] = {
49     0x00, 0x00, 0x10, 0x04,  // nal
50     0x34, 0x12, 0x12, 0x3C,  // lui   s2, high(diff); placeholder = 0x1234
51     0x21, 0x90, 0x5F, 0x02,  // addu  s2, s2, ra
52     0x78, 0x56, 0x52, 0x26,  // addiu s2, s2, low(diff); placeholder = 0x5678
53 };
54 const uint32_t MipsRelativePatcherTest::kLiteralOffset = 4;  // At lui (where patching starts).
55 const uint32_t MipsRelativePatcherTest::kAnchorOffset = 8;  // At addu (where PC+0 points).
56 const ArrayRef<const uint8_t> MipsRelativePatcherTest::kUnpatchedPcRelativeCode(
57     kUnpatchedPcRelativeRawCode);
58 
CheckPcRelativePatch(const ArrayRef<const LinkerPatch> & patches,uint32_t target_offset)59 void MipsRelativePatcherTest::CheckPcRelativePatch(const ArrayRef<const LinkerPatch>& patches,
60                                                    uint32_t target_offset) {
61   AddCompiledMethod(MethodRef(1u), kUnpatchedPcRelativeCode, ArrayRef<const LinkerPatch>(patches));
62   Link();
63 
64   auto result = method_offset_map_.FindMethodOffset(MethodRef(1u));
65   ASSERT_TRUE(result.first);
66 
67   uint32_t diff = target_offset - (result.second + kAnchorOffset);
68   if (patches[0].GetType() == LinkerPatch::Type::kDexCacheArray) {
69     diff += kDexCacheArrayLwOffset;
70   }
71   diff += (diff & 0x8000) << 1;  // Account for sign extension in addiu.
72 
73   const uint8_t expected_code[] = {
74       0x00, 0x00, 0x10, 0x04,
75       static_cast<uint8_t>(diff >> 16), static_cast<uint8_t>(diff >> 24), 0x12, 0x3C,
76       0x21, 0x90, 0x5F, 0x02,
77       static_cast<uint8_t>(diff), static_cast<uint8_t>(diff >> 8), 0x52, 0x26,
78   };
79   EXPECT_TRUE(CheckLinkedMethod(MethodRef(1u), ArrayRef<const uint8_t>(expected_code)));
80 }
81 
TestDexCacheReference(uint32_t dex_cache_arrays_begin,uint32_t element_offset)82 void MipsRelativePatcherTest::TestDexCacheReference(uint32_t dex_cache_arrays_begin,
83                                                     uint32_t element_offset) {
84   dex_cache_arrays_begin_ = dex_cache_arrays_begin;
85   LinkerPatch patches[] = {
86       LinkerPatch::DexCacheArrayPatch(kLiteralOffset, nullptr, kAnchorOffset, element_offset)
87   };
88   CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches),
89                        dex_cache_arrays_begin_ + element_offset);
90 }
91 
TestStringReference(uint32_t string_offset)92 void MipsRelativePatcherTest::TestStringReference(uint32_t string_offset) {
93   constexpr uint32_t kStringIndex = 1u;
94   string_index_to_offset_map_.Put(kStringIndex, string_offset);
95   LinkerPatch patches[] = {
96       LinkerPatch::RelativeStringPatch(kLiteralOffset, nullptr, kAnchorOffset, kStringIndex)
97   };
98   CheckPcRelativePatch(ArrayRef<const LinkerPatch>(patches), string_offset);
99 }
100 
TEST_F(MipsRelativePatcherTest,DexCacheReference)101 TEST_F(MipsRelativePatcherTest, DexCacheReference) {
102   TestDexCacheReference(/* dex_cache_arrays_begin */ 0x12345678, /* element_offset */ 0x1234);
103 }
104 
TEST_F(MipsRelativePatcherTest,StringReference)105 TEST_F(MipsRelativePatcherTest, StringReference) {
106   TestStringReference(/* string_offset*/ 0x87651234);
107 }
108 
109 }  // namespace linker
110 }  // namespace art
111