blob: bf8e786f64d27edd46bd0c22c1adb51a99b4075d [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"
22#include "base/macros.h"
23#include "compiled_method.h"
24#include "dex/quick/dex_file_to_method_inliner_map.h"
25#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 "utils/array_ref.h"
35#include "vector_output_stream.h"
36
37namespace art {
38namespace linker {
39
40// Base class providing infrastructure for architecture-specific tests.
41class RelativePatcherTest : public testing::Test {
42 protected:
43 RelativePatcherTest(InstructionSet instruction_set, const std::string& variant)
44 : compiler_options_(),
45 verification_results_(&compiler_options_),
46 inliner_map_(),
Vladimir Marko45724f92016-02-17 17:46:10 +000047 driver_(&compiler_options_, &verification_results_, &inliner_map_,
48 Compiler::kQuick, instruction_set, nullptr,
49 false, nullptr, nullptr, nullptr, 1u,
50 false, false, nullptr, -1, nullptr, nullptr),
Vladimir Markob163bb72015-03-31 21:49:49 +010051 error_msg_(),
52 instruction_set_(instruction_set),
53 features_(InstructionSetFeatures::FromVariant(instruction_set, variant, &error_msg_)),
54 method_offset_map_(),
55 patcher_(RelativePatcher::Create(instruction_set, features_.get(), &method_offset_map_)),
56 dex_cache_arrays_begin_(0u),
57 compiled_method_refs_(),
58 compiled_methods_(),
59 patched_code_(),
60 output_(),
61 out_("test output stream", &output_) {
62 CHECK(error_msg_.empty()) << instruction_set << "/" << variant;
63 patched_code_.reserve(16 * KB);
64 }
65
66 MethodReference MethodRef(uint32_t method_idx) {
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010067 CHECK_NE(method_idx, 0u);
Vladimir Markob163bb72015-03-31 21:49:49 +010068 return MethodReference(nullptr, method_idx);
69 }
70
71 void AddCompiledMethod(MethodReference method_ref,
72 const ArrayRef<const uint8_t>& code,
Vladimir Markob207e142015-04-02 21:25:21 +010073 const ArrayRef<const LinkerPatch>& patches) {
Vladimir Markob163bb72015-03-31 21:49:49 +010074 compiled_method_refs_.push_back(method_ref);
75 compiled_methods_.emplace_back(new CompiledMethod(
76 &driver_, instruction_set_, code,
Vladimir Marko35831e82015-09-11 11:59:18 +010077 0u, 0u, 0u, ArrayRef<const SrcMapElem>(), ArrayRef<const uint8_t>(),
78 ArrayRef<const uint8_t>(), ArrayRef<const uint8_t>(), ArrayRef<const uint8_t>(),
Vladimir Markob163bb72015-03-31 21:49:49 +010079 patches));
80 }
81
82 void Link() {
83 // Reserve space.
84 static_assert(kTrampolineOffset == 0u, "Unexpected trampoline offset.");
85 uint32_t offset = kTrampolineSize;
86 size_t idx = 0u;
87 for (auto& compiled_method : compiled_methods_) {
Vladimir Marko4d23c9d2015-04-01 23:03:09 +010088 offset = patcher_->ReserveSpace(offset, compiled_method.get(), compiled_method_refs_[idx]);
Vladimir Markob163bb72015-03-31 21:49:49 +010089
90 uint32_t aligned_offset = compiled_method->AlignCode(offset);
91 uint32_t aligned_code_delta = aligned_offset - offset;
92 offset += aligned_code_delta;
93
94 offset += sizeof(OatQuickMethodHeader);
95 uint32_t quick_code_offset = offset + compiled_method->CodeDelta();
Vladimir Marko35831e82015-09-11 11:59:18 +010096 const auto code = compiled_method->GetQuickCode();
Vladimir Markob163bb72015-03-31 21:49:49 +010097 offset += code.size();
98
99 method_offset_map_.map.Put(compiled_method_refs_[idx], quick_code_offset);
100 ++idx;
101 }
Vladimir Marko71b0ddf2015-04-02 19:45:06 +0100102 offset = patcher_->ReserveSpaceEnd(offset);
Vladimir Markob163bb72015-03-31 21:49:49 +0100103 uint32_t output_size = offset;
104 output_.reserve(output_size);
105
106 // Write data.
107 DCHECK(output_.empty());
108 uint8_t dummy_trampoline[kTrampolineSize];
109 memset(dummy_trampoline, 0, sizeof(dummy_trampoline));
110 out_.WriteFully(dummy_trampoline, kTrampolineSize);
111 offset = kTrampolineSize;
112 static const uint8_t kPadding[] = {
113 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
114 };
115 uint8_t dummy_header[sizeof(OatQuickMethodHeader)];
116 memset(dummy_header, 0, sizeof(dummy_header));
117 for (auto& compiled_method : compiled_methods_) {
118 offset = patcher_->WriteThunks(&out_, offset);
119
120 uint32_t aligned_offset = compiled_method->AlignCode(offset);
121 uint32_t aligned_code_delta = aligned_offset - offset;
122 CHECK_LE(aligned_code_delta, sizeof(kPadding));
123 out_.WriteFully(kPadding, aligned_code_delta);
124 offset += aligned_code_delta;
125
126 out_.WriteFully(dummy_header, sizeof(OatQuickMethodHeader));
127 offset += sizeof(OatQuickMethodHeader);
Vladimir Marko35831e82015-09-11 11:59:18 +0100128 ArrayRef<const uint8_t> code = compiled_method->GetQuickCode();
Vladimir Markob163bb72015-03-31 21:49:49 +0100129 if (!compiled_method->GetPatches().empty()) {
130 patched_code_.assign(code.begin(), code.end());
131 code = ArrayRef<const uint8_t>(patched_code_);
132 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
133 if (patch.Type() == kLinkerPatchCallRelative) {
134 auto result = method_offset_map_.FindMethodOffset(patch.TargetMethod());
135 uint32_t target_offset =
136 result.first ? result.second : kTrampolineOffset + compiled_method->CodeDelta();
137 patcher_->PatchCall(&patched_code_, patch.LiteralOffset(),
138 offset + patch.LiteralOffset(), target_offset);
139 } else if (patch.Type() == kLinkerPatchDexCacheArray) {
140 uint32_t target_offset = dex_cache_arrays_begin_ + patch.TargetDexCacheElementOffset();
141 patcher_->PatchDexCacheReference(&patched_code_, patch,
142 offset + patch.LiteralOffset(), target_offset);
143 } else {
144 LOG(FATAL) << "Bad patch type.";
145 }
146 }
147 }
148 out_.WriteFully(&code[0], code.size());
149 offset += code.size();
150 }
151 offset = patcher_->WriteThunks(&out_, offset);
152 CHECK_EQ(offset, output_size);
153 CHECK_EQ(output_.size(), output_size);
154 }
155
156 bool CheckLinkedMethod(MethodReference method_ref, const ArrayRef<const uint8_t>& expected_code) {
157 // Sanity check: original code size must match linked_code.size().
158 size_t idx = 0u;
159 for (auto ref : compiled_method_refs_) {
160 if (ref.dex_file == method_ref.dex_file &&
161 ref.dex_method_index == method_ref.dex_method_index) {
162 break;
163 }
164 ++idx;
165 }
166 CHECK_NE(idx, compiled_method_refs_.size());
Vladimir Marko35831e82015-09-11 11:59:18 +0100167 CHECK_EQ(compiled_methods_[idx]->GetQuickCode().size(), expected_code.size());
Vladimir Markob163bb72015-03-31 21:49:49 +0100168
169 auto result = method_offset_map_.FindMethodOffset(method_ref);
170 CHECK(result.first); // Must have been linked.
Vladimir Marko4d23c9d2015-04-01 23:03:09 +0100171 size_t offset = result.second - compiled_methods_[idx]->CodeDelta();
Vladimir Markob163bb72015-03-31 21:49:49 +0100172 CHECK_LT(offset, output_.size());
173 CHECK_LE(offset + expected_code.size(), output_.size());
174 ArrayRef<const uint8_t> linked_code(&output_[offset], expected_code.size());
175 if (linked_code == expected_code) {
176 return true;
177 }
178 // Log failure info.
Vladimir Marko4d23c9d2015-04-01 23:03:09 +0100179 DumpDiff(expected_code, linked_code);
180 return false;
181 }
182
183 void DumpDiff(const ArrayRef<const uint8_t>& expected_code,
184 const ArrayRef<const uint8_t>& linked_code) {
Vladimir Markob163bb72015-03-31 21:49:49 +0100185 std::ostringstream expected_hex;
186 std::ostringstream linked_hex;
187 std::ostringstream diff_indicator;
188 static const char digits[] = "0123456789abcdef";
189 bool found_diff = false;
190 for (size_t i = 0; i != expected_code.size(); ++i) {
191 expected_hex << " " << digits[expected_code[i] >> 4] << digits[expected_code[i] & 0xf];
192 linked_hex << " " << digits[linked_code[i] >> 4] << digits[linked_code[i] & 0xf];
Vladimir Markob163bb72015-03-31 21:49:49 +0100193 if (!found_diff) {
194 found_diff = (expected_code[i] != linked_code[i]);
Vladimir Marko3f311cf2015-04-02 15:28:45 +0100195 diff_indicator << (found_diff ? " ^^" : " ");
Vladimir Markob163bb72015-03-31 21:49:49 +0100196 }
197 }
198 CHECK(found_diff);
Vladimir Marko3f311cf2015-04-02 15:28:45 +0100199 std::string expected_hex_str = expected_hex.str();
200 std::string linked_hex_str = linked_hex.str();
201 std::string diff_indicator_str = diff_indicator.str();
202 if (diff_indicator_str.length() > 60) {
203 CHECK_EQ(diff_indicator_str.length() % 3u, 0u);
204 size_t remove = diff_indicator_str.length() / 3 - 5;
205 std::ostringstream oss;
206 oss << "[stripped " << remove << "]";
207 std::string replacement = oss.str();
208 expected_hex_str.replace(0u, remove * 3u, replacement);
209 linked_hex_str.replace(0u, remove * 3u, replacement);
210 diff_indicator_str.replace(0u, remove * 3u, replacement);
211 }
Vladimir Markob163bb72015-03-31 21:49:49 +0100212 LOG(ERROR) << "diff expected_code linked_code";
Vladimir Marko3f311cf2015-04-02 15:28:45 +0100213 LOG(ERROR) << "<" << expected_hex_str;
214 LOG(ERROR) << ">" << linked_hex_str;
215 LOG(ERROR) << " " << diff_indicator_str;
Vladimir Markob163bb72015-03-31 21:49:49 +0100216 }
217
218 // Map method reference to assinged offset.
219 // Wrap the map in a class implementing linker::RelativePatcherTargetProvider.
220 class MethodOffsetMap FINAL : public linker::RelativePatcherTargetProvider {
221 public:
222 std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE {
223 auto it = map.find(ref);
224 if (it == map.end()) {
225 return std::pair<bool, uint32_t>(false, 0u);
226 } else {
227 return std::pair<bool, uint32_t>(true, it->second);
228 }
229 }
230 SafeMap<MethodReference, uint32_t, MethodReferenceComparator> map;
231 };
232
233 static const uint32_t kTrampolineSize = 4u;
234 static const uint32_t kTrampolineOffset = 0u;
235
236 CompilerOptions compiler_options_;
237 VerificationResults verification_results_;
238 DexFileToMethodInlinerMap inliner_map_;
239 CompilerDriver driver_; // Needed for constructing CompiledMethod.
240 std::string error_msg_;
241 InstructionSet instruction_set_;
242 std::unique_ptr<const InstructionSetFeatures> features_;
243 MethodOffsetMap method_offset_map_;
244 std::unique_ptr<RelativePatcher> patcher_;
245 uint32_t dex_cache_arrays_begin_;
246 std::vector<MethodReference> compiled_method_refs_;
247 std::vector<std::unique_ptr<CompiledMethod>> compiled_methods_;
248 std::vector<uint8_t> patched_code_;
249 std::vector<uint8_t> output_;
250 VectorOutputStream out_;
251};
252
253} // namespace linker
254} // namespace art
255
256#endif // ART_COMPILER_LINKER_RELATIVE_PATCHER_TEST_H_