blob: 615b2b97be3f06bf244fef298ad438f7c4172bd0 [file] [log] [blame]
Vladimir Marko944da602016-02-19 12:27:55 +00001/*
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 "compiled_method.h"
18#include "gtest/gtest.h"
19#include "multi_oat_relative_patcher.h"
20#include "vector_output_stream.h"
21
22namespace art {
23namespace linker {
24
25static const MethodReference kNullMethodRef = MethodReference(nullptr, 0u);
26
27static bool EqualRef(MethodReference lhs, MethodReference rhs) {
28 return lhs.dex_file == rhs.dex_file && lhs.dex_method_index == rhs.dex_method_index;
29}
30
31class MultiOatRelativePatcherTest : public testing::Test {
32 protected:
33 class MockPatcher : public RelativePatcher {
34 public:
35 MockPatcher() { }
36
37 uint32_t ReserveSpace(uint32_t offset,
38 const CompiledMethod* compiled_method ATTRIBUTE_UNUSED,
39 MethodReference method_ref) OVERRIDE {
40 last_reserve_offset_ = offset;
41 last_reserve_method_ = method_ref;
42 offset += next_reserve_adjustment_;
43 next_reserve_adjustment_ = 0u;
44 return offset;
45 }
46
47 uint32_t ReserveSpaceEnd(uint32_t offset) OVERRIDE {
48 last_reserve_offset_ = offset;
49 last_reserve_method_ = kNullMethodRef;
50 offset += next_reserve_adjustment_;
51 next_reserve_adjustment_ = 0u;
52 return offset;
53 }
54
55 uint32_t WriteThunks(OutputStream* out, uint32_t offset) OVERRIDE {
56 last_write_offset_ = offset;
57 if (next_write_alignment_ != 0u) {
58 offset += next_write_alignment_;
59 bool success = WriteCodeAlignment(out, next_write_alignment_);
60 CHECK(success);
61 next_write_alignment_ = 0u;
62 }
63 if (next_write_call_thunk_ != 0u) {
64 offset += next_write_call_thunk_;
65 std::vector<uint8_t> thunk(next_write_call_thunk_, 'c');
Vladimir Markof4f2daa2017-03-20 18:26:59 +000066 bool success = WriteThunk(out, ArrayRef<const uint8_t>(thunk));
Vladimir Marko944da602016-02-19 12:27:55 +000067 CHECK(success);
68 next_write_call_thunk_ = 0u;
69 }
70 if (next_write_misc_thunk_ != 0u) {
71 offset += next_write_misc_thunk_;
72 std::vector<uint8_t> thunk(next_write_misc_thunk_, 'm');
73 bool success = WriteMiscThunk(out, ArrayRef<const uint8_t>(thunk));
74 CHECK(success);
75 next_write_misc_thunk_ = 0u;
76 }
77 return offset;
78 }
79
80 void PatchCall(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
81 uint32_t literal_offset,
82 uint32_t patch_offset,
83 uint32_t target_offset) OVERRIDE {
84 last_literal_offset_ = literal_offset;
85 last_patch_offset_ = patch_offset;
86 last_target_offset_ = target_offset;
87 }
88
Vladimir Markocac5a7e2016-02-22 10:39:50 +000089 void PatchPcRelativeReference(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
90 const LinkerPatch& patch,
91 uint32_t patch_offset,
92 uint32_t target_offset) OVERRIDE {
Vladimir Marko944da602016-02-19 12:27:55 +000093 last_literal_offset_ = patch.LiteralOffset();
94 last_patch_offset_ = patch_offset;
95 last_target_offset_ = target_offset;
96 }
97
Vladimir Markof4f2daa2017-03-20 18:26:59 +000098 void PatchBakerReadBarrierBranch(std::vector<uint8_t>* code ATTRIBUTE_UNUSED,
99 const LinkerPatch& patch ATTRIBUTE_UNUSED,
100 uint32_t patch_offset ATTRIBUTE_UNUSED) {
101 LOG(FATAL) << "UNIMPLEMENTED";
102 }
103
Vladimir Marko944da602016-02-19 12:27:55 +0000104 uint32_t last_reserve_offset_ = 0u;
105 MethodReference last_reserve_method_ = kNullMethodRef;
106 uint32_t next_reserve_adjustment_ = 0u;
107
108 uint32_t last_write_offset_ = 0u;
109 uint32_t next_write_alignment_ = 0u;
110 uint32_t next_write_call_thunk_ = 0u;
111 uint32_t next_write_misc_thunk_ = 0u;
112
113 uint32_t last_literal_offset_ = 0u;
114 uint32_t last_patch_offset_ = 0u;
115 uint32_t last_target_offset_ = 0u;
116 };
117
118 MultiOatRelativePatcherTest()
119 : instruction_set_features_(InstructionSetFeatures::FromCppDefines()),
120 patcher_(kRuntimeISA, instruction_set_features_.get()) {
121 std::unique_ptr<MockPatcher> mock(new MockPatcher());
122 mock_ = mock.get();
123 patcher_.relative_patcher_ = std::move(mock);
124 }
125
126 std::unique_ptr<const InstructionSetFeatures> instruction_set_features_;
127 MultiOatRelativePatcher patcher_;
128 MockPatcher* mock_;
129};
130
131TEST_F(MultiOatRelativePatcherTest, Offsets) {
132 const DexFile* dex_file = reinterpret_cast<const DexFile*>(1);
133 MethodReference ref1(dex_file, 1u);
134 MethodReference ref2(dex_file, 2u);
135 EXPECT_EQ(0u, patcher_.GetOffset(ref1));
136 EXPECT_EQ(0u, patcher_.GetOffset(ref2));
137
138 uint32_t adjustment1 = 0x1000;
139 patcher_.StartOatFile(adjustment1);
140 EXPECT_EQ(0u, patcher_.GetOffset(ref1));
141 EXPECT_EQ(0u, patcher_.GetOffset(ref2));
142
143 uint32_t off1 = 0x1234;
144 patcher_.SetOffset(ref1, off1);
145 EXPECT_EQ(off1, patcher_.GetOffset(ref1));
146 EXPECT_EQ(0u, patcher_.GetOffset(ref2));
147
148 uint32_t adjustment2 = 0x30000;
149 patcher_.StartOatFile(adjustment2);
150 EXPECT_EQ(off1 + adjustment1 - adjustment2, patcher_.GetOffset(ref1));
151 EXPECT_EQ(0u, patcher_.GetOffset(ref2));
152
153 uint32_t off2 = 0x4321;
154 patcher_.SetOffset(ref2, off2);
155 EXPECT_EQ(off1 + adjustment1 - adjustment2, patcher_.GetOffset(ref1));
156 EXPECT_EQ(off2, patcher_.GetOffset(ref2));
157
158 uint32_t adjustment3 = 0x78000;
159 patcher_.StartOatFile(adjustment3);
160 EXPECT_EQ(off1 + adjustment1 - adjustment3, patcher_.GetOffset(ref1));
161 EXPECT_EQ(off2 + adjustment2 - adjustment3, patcher_.GetOffset(ref2));
162}
163
164TEST_F(MultiOatRelativePatcherTest, OffsetsInReserve) {
165 const DexFile* dex_file = reinterpret_cast<const DexFile*>(1);
166 MethodReference ref1(dex_file, 1u);
167 MethodReference ref2(dex_file, 2u);
168 MethodReference ref3(dex_file, 3u);
169 const CompiledMethod* method = reinterpret_cast<const CompiledMethod*>(-1);
170
171 uint32_t adjustment1 = 0x1000;
172 patcher_.StartOatFile(adjustment1);
173
174 uint32_t method1_offset = 0x100;
175 uint32_t method1_offset_check = patcher_.ReserveSpace(method1_offset, method, ref1);
176 ASSERT_EQ(adjustment1 + method1_offset, mock_->last_reserve_offset_);
177 ASSERT_TRUE(EqualRef(ref1, mock_->last_reserve_method_));
178 ASSERT_EQ(method1_offset, method1_offset_check);
179
180 uint32_t method2_offset = 0x1230;
181 uint32_t method2_reserve_adjustment = 0x10;
182 mock_->next_reserve_adjustment_ = method2_reserve_adjustment;
183 uint32_t method2_offset_adjusted = patcher_.ReserveSpace(method2_offset, method, ref2);
184 ASSERT_EQ(adjustment1 + method2_offset, mock_->last_reserve_offset_);
185 ASSERT_TRUE(EqualRef(ref2, mock_->last_reserve_method_));
186 ASSERT_EQ(method2_offset + method2_reserve_adjustment, method2_offset_adjusted);
187
188 uint32_t end1_offset = 0x4320;
189 uint32_t end1_offset_check = patcher_.ReserveSpaceEnd(end1_offset);
190 ASSERT_EQ(adjustment1 + end1_offset, mock_->last_reserve_offset_);
191 ASSERT_TRUE(EqualRef(kNullMethodRef, mock_->last_reserve_method_));
192 ASSERT_EQ(end1_offset, end1_offset_check);
193
194 uint32_t adjustment2 = 0xd000;
195 patcher_.StartOatFile(adjustment2);
196
197 uint32_t method3_offset = 0xf00;
198 uint32_t method3_offset_check = patcher_.ReserveSpace(method3_offset, method, ref3);
199 ASSERT_EQ(adjustment2 + method3_offset, mock_->last_reserve_offset_);
200 ASSERT_TRUE(EqualRef(ref3, mock_->last_reserve_method_));
201 ASSERT_EQ(method3_offset, method3_offset_check);
202
203 uint32_t end2_offset = 0x2400;
204 uint32_t end2_reserve_adjustment = 0x20;
205 mock_->next_reserve_adjustment_ = end2_reserve_adjustment;
206 uint32_t end2_offset_adjusted = patcher_.ReserveSpaceEnd(end2_offset);
207 ASSERT_EQ(adjustment2 + end2_offset, mock_->last_reserve_offset_);
208 ASSERT_TRUE(EqualRef(kNullMethodRef, mock_->last_reserve_method_));
209 ASSERT_EQ(end2_offset + end2_reserve_adjustment, end2_offset_adjusted);
210}
211
212TEST_F(MultiOatRelativePatcherTest, Write) {
213 std::vector<uint8_t> output;
214 VectorOutputStream vos("output", &output);
215
216 uint32_t adjustment1 = 0x1000;
217 patcher_.StartOatFile(adjustment1);
218
219 uint32_t method1_offset = 0x100;
220 uint32_t method1_offset_check = patcher_.WriteThunks(&vos, method1_offset);
221 ASSERT_EQ(adjustment1 + method1_offset, mock_->last_write_offset_);
222 ASSERT_EQ(method1_offset, method1_offset_check);
223 vos.WriteFully("1", 1); // Mark method1.
224
225 uint32_t method2_offset = 0x1230;
226 uint32_t method2_alignment_size = 1;
227 uint32_t method2_call_thunk_size = 2;
228 mock_->next_write_alignment_ = method2_alignment_size;
229 mock_->next_write_call_thunk_ = method2_call_thunk_size;
230 uint32_t method2_offset_adjusted = patcher_.WriteThunks(&vos, method2_offset);
231 ASSERT_EQ(adjustment1 + method2_offset, mock_->last_write_offset_);
232 ASSERT_EQ(method2_offset + method2_alignment_size + method2_call_thunk_size,
233 method2_offset_adjusted);
234 vos.WriteFully("2", 1); // Mark method2.
235
236 EXPECT_EQ(method2_alignment_size, patcher_.CodeAlignmentSize());
237 EXPECT_EQ(method2_call_thunk_size, patcher_.RelativeCallThunksSize());
238
239 uint32_t adjustment2 = 0xd000;
240 patcher_.StartOatFile(adjustment2);
241
242 uint32_t method3_offset = 0xf00;
243 uint32_t method3_alignment_size = 2;
244 uint32_t method3_misc_thunk_size = 1;
245 mock_->next_write_alignment_ = method3_alignment_size;
246 mock_->next_write_misc_thunk_ = method3_misc_thunk_size;
247 uint32_t method3_offset_adjusted = patcher_.WriteThunks(&vos, method3_offset);
248 ASSERT_EQ(adjustment2 + method3_offset, mock_->last_write_offset_);
249 ASSERT_EQ(method3_offset + method3_alignment_size + method3_misc_thunk_size,
250 method3_offset_adjusted);
251 vos.WriteFully("3", 1); // Mark method3.
252
253 EXPECT_EQ(method3_alignment_size, patcher_.CodeAlignmentSize());
254 EXPECT_EQ(method3_misc_thunk_size, patcher_.MiscThunksSize());
255
256 uint8_t expected_output[] = {
257 '1',
258 0, 'c', 'c', '2',
259 0, 0, 'm', '3',
260 };
261 ASSERT_EQ(arraysize(expected_output), output.size());
262 for (size_t i = 0; i != arraysize(expected_output); ++i) {
263 ASSERT_EQ(expected_output[i], output[i]) << i;
264 }
265}
266
267TEST_F(MultiOatRelativePatcherTest, Patch) {
268 std::vector<uint8_t> code(16);
269
270 uint32_t adjustment1 = 0x1000;
271 patcher_.StartOatFile(adjustment1);
272
273 uint32_t method1_literal_offset = 4u;
274 uint32_t method1_patch_offset = 0x1234u;
275 uint32_t method1_target_offset = 0x8888u;
276 patcher_.PatchCall(&code, method1_literal_offset, method1_patch_offset, method1_target_offset);
277 DCHECK_EQ(method1_literal_offset, mock_->last_literal_offset_);
278 DCHECK_EQ(method1_patch_offset + adjustment1, mock_->last_patch_offset_);
279 DCHECK_EQ(method1_target_offset + adjustment1, mock_->last_target_offset_);
280
281 uint32_t method2_literal_offset = 12u;
282 uint32_t method2_patch_offset = 0x7654u;
283 uint32_t method2_target_offset = 0xccccu;
284 LinkerPatch method2_patch =
Vladimir Marko5f078202017-05-18 13:32:53 +0100285 LinkerPatch::StringBssEntryPatch(method2_literal_offset, nullptr, 0u, 1u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000286 patcher_.PatchPcRelativeReference(
Vladimir Marko944da602016-02-19 12:27:55 +0000287 &code, method2_patch, method2_patch_offset, method2_target_offset);
288 DCHECK_EQ(method2_literal_offset, mock_->last_literal_offset_);
289 DCHECK_EQ(method2_patch_offset + adjustment1, mock_->last_patch_offset_);
290 DCHECK_EQ(method2_target_offset + adjustment1, mock_->last_target_offset_);
291
292 uint32_t adjustment2 = 0xd000;
293 patcher_.StartOatFile(adjustment2);
294
295 uint32_t method3_literal_offset = 8u;
296 uint32_t method3_patch_offset = 0x108u;
297 uint32_t method3_target_offset = 0x200u;
298 patcher_.PatchCall(&code, method3_literal_offset, method3_patch_offset, method3_target_offset);
299 DCHECK_EQ(method3_literal_offset, mock_->last_literal_offset_);
300 DCHECK_EQ(method3_patch_offset + adjustment2, mock_->last_patch_offset_);
301 DCHECK_EQ(method3_target_offset + adjustment2, mock_->last_target_offset_);
302}
303
304} // namespace linker
305} // namespace art