blob: 233daf4a39a22f0e3667296bf6dcb67d281644be [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#ifndef ART_COMPILER_LINKER_RELATIVE_PATCHER_TEST_H_
18#define ART_COMPILER_LINKER_RELATIVE_PATCHER_TEST_H_
19
20#include "arch/instruction_set.h"
21#include "arch/instruction_set_features.h"
David Brazdild9c90372016-09-14 16:53:55 +010022#include "base/array_ref.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010023#include "base/macros.h"
24#include "compiled_method.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010025#include "dex/verification_results.h"
26#include "driver/compiler_driver.h"
27#include "driver/compiler_options.h"
28#include "globals.h"
29#include "gtest/gtest.h"
30#include "linker/relative_patcher.h"
31#include "method_reference.h"
32#include "oat.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010033#include "oat_quick_method_header.h"
Vladimir Markob163bb72015-03-31 21:49:49 +010034#include "vector_output_stream.h"
35
36namespace art {
37namespace linker {
38
39// Base class providing infrastructure for architecture-specific tests.
40class RelativePatcherTest : public testing::Test {
41 protected:
42 RelativePatcherTest(InstructionSet instruction_set, const std::string& variant)
43 : compiler_options_(),
44 verification_results_(&compiler_options_),
Vladimir Marko944da602016-02-19 12:27:55 +000045 driver_(&compiler_options_,
46 &verification_results_,
Vladimir Marko944da602016-02-19 12:27:55 +000047 Compiler::kQuick,
48 instruction_set,
49 /* instruction_set_features*/ nullptr,
Vladimir Marko944da602016-02-19 12:27:55 +000050 /* image_classes */ nullptr,
51 /* compiled_classes */ nullptr,
52 /* compiled_methods */ nullptr,
53 /* thread_count */ 1u,
54 /* dump_stats */ false,
55 /* dump_passes */ false,
56 /* timer */ nullptr,
57 /* swap_fd */ -1,
58 /* profile_compilation_info */ nullptr),
Vladimir Markob163bb72015-03-31 21:49:49 +010059 error_msg_(),
60 instruction_set_(instruction_set),
61 features_(InstructionSetFeatures::FromVariant(instruction_set, variant, &error_msg_)),
62 method_offset_map_(),
63 patcher_(RelativePatcher::Create(instruction_set, features_.get(), &method_offset_map_)),
64 dex_cache_arrays_begin_(0u),
65 compiled_method_refs_(),
66 compiled_methods_(),
67 patched_code_(),
68 output_(),
69 out_("test output stream", &output_) {
70 CHECK(error_msg_.empty()) << instruction_set << "/" << variant;
71 patched_code_.reserve(16 * KB);
72 }
73
74 MethodReference MethodRef(uint32_t method_idx) {
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010075 CHECK_NE(method_idx, 0u);
Vladimir Markob163bb72015-03-31 21:49:49 +010076 return MethodReference(nullptr, method_idx);
77 }
78
79 void AddCompiledMethod(MethodReference method_ref,
80 const ArrayRef<const uint8_t>& code,
Vladimir Markob207e142015-04-02 21:25:21 +010081 const ArrayRef<const LinkerPatch>& patches) {
Vladimir Markob163bb72015-03-31 21:49:49 +010082 compiled_method_refs_.push_back(method_ref);
83 compiled_methods_.emplace_back(new CompiledMethod(
Vladimir Marko9d07e3d2016-03-31 12:02:28 +010084 &driver_,
85 instruction_set_,
86 code,
87 /* frame_size_in_bytes */ 0u,
88 /* core_spill_mask */ 0u,
89 /* fp_spill_mask */ 0u,
90 /* src_mapping_table */ ArrayRef<const SrcMapElem>(),
91 /* vmap_table */ ArrayRef<const uint8_t>(),
92 /* cfi_info */ ArrayRef<const uint8_t>(),
Vladimir Markob163bb72015-03-31 21:49:49 +010093 patches));
94 }
95
Vladimir Marko0c737df2016-08-01 16:33:16 +010096 uint32_t CodeAlignmentSize(uint32_t header_offset_to_align) {
97 // We want to align the code rather than the preheader.
98 uint32_t unaligned_code_offset = header_offset_to_align + sizeof(OatQuickMethodHeader);
99 uint32_t aligned_code_offset =
100 CompiledMethod::AlignCode(unaligned_code_offset, instruction_set_);
101 return aligned_code_offset - unaligned_code_offset;
102 }
103
Vladimir Markob163bb72015-03-31 21:49:49 +0100104 void Link() {
105 // Reserve space.
106 static_assert(kTrampolineOffset == 0u, "Unexpected trampoline offset.");
107 uint32_t offset = kTrampolineSize;
108 size_t idx = 0u;
109 for (auto& compiled_method : compiled_methods_) {
Vladimir Marko4d23c9d2015-04-01 23:03:09 +0100110 offset = patcher_->ReserveSpace(offset, compiled_method.get(), compiled_method_refs_[idx]);
Vladimir Markob163bb72015-03-31 21:49:49 +0100111
Vladimir Marko0c737df2016-08-01 16:33:16 +0100112 uint32_t alignment_size = CodeAlignmentSize(offset);
113 offset += alignment_size;
Vladimir Markob163bb72015-03-31 21:49:49 +0100114
115 offset += sizeof(OatQuickMethodHeader);
116 uint32_t quick_code_offset = offset + compiled_method->CodeDelta();
Vladimir Marko35831e82015-09-11 11:59:18 +0100117 const auto code = compiled_method->GetQuickCode();
Vladimir Markob163bb72015-03-31 21:49:49 +0100118 offset += code.size();
119
120 method_offset_map_.map.Put(compiled_method_refs_[idx], quick_code_offset);
121 ++idx;
122 }
Vladimir Marko71b0ddf2015-04-02 19:45:06 +0100123 offset = patcher_->ReserveSpaceEnd(offset);
Vladimir Markob163bb72015-03-31 21:49:49 +0100124 uint32_t output_size = offset;
125 output_.reserve(output_size);
126
127 // Write data.
128 DCHECK(output_.empty());
129 uint8_t dummy_trampoline[kTrampolineSize];
130 memset(dummy_trampoline, 0, sizeof(dummy_trampoline));
131 out_.WriteFully(dummy_trampoline, kTrampolineSize);
132 offset = kTrampolineSize;
133 static const uint8_t kPadding[] = {
134 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
135 };
136 uint8_t dummy_header[sizeof(OatQuickMethodHeader)];
137 memset(dummy_header, 0, sizeof(dummy_header));
138 for (auto& compiled_method : compiled_methods_) {
139 offset = patcher_->WriteThunks(&out_, offset);
140
Vladimir Marko0c737df2016-08-01 16:33:16 +0100141 uint32_t alignment_size = CodeAlignmentSize(offset);
142 CHECK_LE(alignment_size, sizeof(kPadding));
143 out_.WriteFully(kPadding, alignment_size);
144 offset += alignment_size;
Vladimir Markob163bb72015-03-31 21:49:49 +0100145
146 out_.WriteFully(dummy_header, sizeof(OatQuickMethodHeader));
147 offset += sizeof(OatQuickMethodHeader);
Vladimir Marko35831e82015-09-11 11:59:18 +0100148 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
Vladimir Markob163bb72015-03-31 21:49:49 +0100149 if (!compiled_method->GetPatches().empty()) {
150 patched_code_.assign(code.begin(), code.end());
151 code = ArrayRef<const uint8_t>(patched_code_);
152 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100153 if (patch.GetType() == LinkerPatch::Type::kCallRelative) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100154 auto result = method_offset_map_.FindMethodOffset(patch.TargetMethod());
155 uint32_t target_offset =
156 result.first ? result.second : kTrampolineOffset + compiled_method->CodeDelta();
157 patcher_->PatchCall(&patched_code_, patch.LiteralOffset(),
158 offset + patch.LiteralOffset(), target_offset);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100159 } else if (patch.GetType() == LinkerPatch::Type::kDexCacheArray) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100160 uint32_t target_offset = dex_cache_arrays_begin_ + patch.TargetDexCacheElementOffset();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000161 patcher_->PatchPcRelativeReference(&patched_code_,
162 patch,
163 offset + patch.LiteralOffset(),
164 target_offset);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100165 } else if (patch.GetType() == LinkerPatch::Type::kStringRelative) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800166 uint32_t target_offset =
167 string_index_to_offset_map_.Get(patch.TargetStringIndex().index_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000168 patcher_->PatchPcRelativeReference(&patched_code_,
169 patch,
170 offset + patch.LiteralOffset(),
171 target_offset);
Vladimir Markob163bb72015-03-31 21:49:49 +0100172 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100173 LOG(FATAL) << "Bad patch type. " << patch.GetType();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000174 UNREACHABLE();
Vladimir Markob163bb72015-03-31 21:49:49 +0100175 }
176 }
177 }
178 out_.WriteFully(&code[0], code.size());
179 offset += code.size();
180 }
181 offset = patcher_->WriteThunks(&out_, offset);
182 CHECK_EQ(offset, output_size);
183 CHECK_EQ(output_.size(), output_size);
184 }
185
186 bool CheckLinkedMethod(MethodReference method_ref, const ArrayRef<const uint8_t>& expected_code) {
187 // Sanity check: original code size must match linked_code.size().
188 size_t idx = 0u;
189 for (auto ref : compiled_method_refs_) {
190 if (ref.dex_file == method_ref.dex_file &&
191 ref.dex_method_index == method_ref.dex_method_index) {
192 break;
193 }
194 ++idx;
195 }
196 CHECK_NE(idx, compiled_method_refs_.size());
Vladimir Marko35831e82015-09-11 11:59:18 +0100197 CHECK_EQ(compiled_methods_[idx]->GetQuickCode().size(), expected_code.size());
Vladimir Markob163bb72015-03-31 21:49:49 +0100198
199 auto result = method_offset_map_.FindMethodOffset(method_ref);
200 CHECK(result.first); // Must have been linked.
Vladimir Marko4d23c9d2015-04-01 23:03:09 +0100201 size_t offset = result.second - compiled_methods_[idx]->CodeDelta();
Vladimir Markob163bb72015-03-31 21:49:49 +0100202 CHECK_LT(offset, output_.size());
203 CHECK_LE(offset + expected_code.size(), output_.size());
204 ArrayRef<const uint8_t> linked_code(&output_[offset], expected_code.size());
205 if (linked_code == expected_code) {
206 return true;
207 }
208 // Log failure info.
Vladimir Marko4d23c9d2015-04-01 23:03:09 +0100209 DumpDiff(expected_code, linked_code);
210 return false;
211 }
212
213 void DumpDiff(const ArrayRef<const uint8_t>& expected_code,
214 const ArrayRef<const uint8_t>& linked_code) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100215 std::ostringstream expected_hex;
216 std::ostringstream linked_hex;
217 std::ostringstream diff_indicator;
218 static const char digits[] = "0123456789abcdef";
219 bool found_diff = false;
220 for (size_t i = 0; i != expected_code.size(); ++i) {
221 expected_hex << " " << digits[expected_code[i] >> 4] << digits[expected_code[i] & 0xf];
222 linked_hex << " " << digits[linked_code[i] >> 4] << digits[linked_code[i] & 0xf];
Vladimir Markob163bb72015-03-31 21:49:49 +0100223 if (!found_diff) {
224 found_diff = (expected_code[i] != linked_code[i]);
Vladimir Marko3f311cf2015-04-02 15:28:45 +0100225 diff_indicator << (found_diff ? " ^^" : " ");
Vladimir Markob163bb72015-03-31 21:49:49 +0100226 }
227 }
228 CHECK(found_diff);
Vladimir Marko3f311cf2015-04-02 15:28:45 +0100229 std::string expected_hex_str = expected_hex.str();
230 std::string linked_hex_str = linked_hex.str();
231 std::string diff_indicator_str = diff_indicator.str();
232 if (diff_indicator_str.length() > 60) {
233 CHECK_EQ(diff_indicator_str.length() % 3u, 0u);
234 size_t remove = diff_indicator_str.length() / 3 - 5;
235 std::ostringstream oss;
236 oss << "[stripped " << remove << "]";
237 std::string replacement = oss.str();
238 expected_hex_str.replace(0u, remove * 3u, replacement);
239 linked_hex_str.replace(0u, remove * 3u, replacement);
240 diff_indicator_str.replace(0u, remove * 3u, replacement);
241 }
Vladimir Markob163bb72015-03-31 21:49:49 +0100242 LOG(ERROR) << "diff expected_code linked_code";
Vladimir Marko3f311cf2015-04-02 15:28:45 +0100243 LOG(ERROR) << "<" << expected_hex_str;
244 LOG(ERROR) << ">" << linked_hex_str;
245 LOG(ERROR) << " " << diff_indicator_str;
Vladimir Markob163bb72015-03-31 21:49:49 +0100246 }
247
248 // Map method reference to assinged offset.
249 // Wrap the map in a class implementing linker::RelativePatcherTargetProvider.
250 class MethodOffsetMap FINAL : public linker::RelativePatcherTargetProvider {
251 public:
252 std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE {
253 auto it = map.find(ref);
254 if (it == map.end()) {
255 return std::pair<bool, uint32_t>(false, 0u);
256 } else {
257 return std::pair<bool, uint32_t>(true, it->second);
258 }
259 }
260 SafeMap<MethodReference, uint32_t, MethodReferenceComparator> map;
261 };
262
263 static const uint32_t kTrampolineSize = 4u;
264 static const uint32_t kTrampolineOffset = 0u;
265
266 CompilerOptions compiler_options_;
267 VerificationResults verification_results_;
Vladimir Markob163bb72015-03-31 21:49:49 +0100268 CompilerDriver driver_; // Needed for constructing CompiledMethod.
269 std::string error_msg_;
270 InstructionSet instruction_set_;
271 std::unique_ptr<const InstructionSetFeatures> features_;
272 MethodOffsetMap method_offset_map_;
273 std::unique_ptr<RelativePatcher> patcher_;
274 uint32_t dex_cache_arrays_begin_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000275 SafeMap<uint32_t, uint32_t> string_index_to_offset_map_;
Vladimir Markob163bb72015-03-31 21:49:49 +0100276 std::vector<MethodReference> compiled_method_refs_;
277 std::vector<std::unique_ptr<CompiledMethod>> compiled_methods_;
278 std::vector<uint8_t> patched_code_;
279 std::vector<uint8_t> output_;
280 VectorOutputStream out_;
281};
282
283} // namespace linker
284} // namespace art
285
286#endif // ART_COMPILER_LINKER_RELATIVE_PATCHER_TEST_H_