blob: 515355be0e1d01880182d6c158eb76cc8397a631 [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"
33#include "utils/array_ref.h"
34#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_),
45 inliner_map_(),
46 driver_(&compiler_options_, &verification_results_, &inliner_map_,
47 Compiler::kQuick, instruction_set, nullptr,
48 false, nullptr, nullptr, 1u,
49 false, false, "", nullptr, -1, ""),
50 error_msg_(),
51 instruction_set_(instruction_set),
52 features_(InstructionSetFeatures::FromVariant(instruction_set, variant, &error_msg_)),
53 method_offset_map_(),
54 patcher_(RelativePatcher::Create(instruction_set, features_.get(), &method_offset_map_)),
55 dex_cache_arrays_begin_(0u),
56 compiled_method_refs_(),
57 compiled_methods_(),
58 patched_code_(),
59 output_(),
60 out_("test output stream", &output_) {
61 CHECK(error_msg_.empty()) << instruction_set << "/" << variant;
62 patched_code_.reserve(16 * KB);
63 }
64
65 MethodReference MethodRef(uint32_t method_idx) {
66 return MethodReference(nullptr, method_idx);
67 }
68
69 void AddCompiledMethod(MethodReference method_ref,
70 const ArrayRef<const uint8_t>& code,
71 const ArrayRef<LinkerPatch>& patches) {
72 compiled_method_refs_.push_back(method_ref);
73 compiled_methods_.emplace_back(new CompiledMethod(
74 &driver_, instruction_set_, code,
75 0u, 0u, 0u, nullptr, ArrayRef<const uint8_t>(), ArrayRef<const uint8_t>(),
76 ArrayRef<const uint8_t>(), ArrayRef<const uint8_t>(),
77 patches));
78 }
79
80 void Link() {
81 // Reserve space.
82 static_assert(kTrampolineOffset == 0u, "Unexpected trampoline offset.");
83 uint32_t offset = kTrampolineSize;
84 size_t idx = 0u;
85 for (auto& compiled_method : compiled_methods_) {
86 offset = patcher_->ReserveSpace(offset, compiled_method.get());
87
88 uint32_t aligned_offset = compiled_method->AlignCode(offset);
89 uint32_t aligned_code_delta = aligned_offset - offset;
90 offset += aligned_code_delta;
91
92 offset += sizeof(OatQuickMethodHeader);
93 uint32_t quick_code_offset = offset + compiled_method->CodeDelta();
94 const auto& code = *compiled_method->GetQuickCode();
95 offset += code.size();
96
97 method_offset_map_.map.Put(compiled_method_refs_[idx], quick_code_offset);
98 ++idx;
99 }
100 offset = patcher_->ReserveSpace(offset, nullptr);
101 uint32_t output_size = offset;
102 output_.reserve(output_size);
103
104 // Write data.
105 DCHECK(output_.empty());
106 uint8_t dummy_trampoline[kTrampolineSize];
107 memset(dummy_trampoline, 0, sizeof(dummy_trampoline));
108 out_.WriteFully(dummy_trampoline, kTrampolineSize);
109 offset = kTrampolineSize;
110 static const uint8_t kPadding[] = {
111 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u
112 };
113 uint8_t dummy_header[sizeof(OatQuickMethodHeader)];
114 memset(dummy_header, 0, sizeof(dummy_header));
115 for (auto& compiled_method : compiled_methods_) {
116 offset = patcher_->WriteThunks(&out_, offset);
117
118 uint32_t aligned_offset = compiled_method->AlignCode(offset);
119 uint32_t aligned_code_delta = aligned_offset - offset;
120 CHECK_LE(aligned_code_delta, sizeof(kPadding));
121 out_.WriteFully(kPadding, aligned_code_delta);
122 offset += aligned_code_delta;
123
124 out_.WriteFully(dummy_header, sizeof(OatQuickMethodHeader));
125 offset += sizeof(OatQuickMethodHeader);
126 ArrayRef<const uint8_t> code(*compiled_method->GetQuickCode());
127 if (!compiled_method->GetPatches().empty()) {
128 patched_code_.assign(code.begin(), code.end());
129 code = ArrayRef<const uint8_t>(patched_code_);
130 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
131 if (patch.Type() == kLinkerPatchCallRelative) {
132 auto result = method_offset_map_.FindMethodOffset(patch.TargetMethod());
133 uint32_t target_offset =
134 result.first ? result.second : kTrampolineOffset + compiled_method->CodeDelta();
135 patcher_->PatchCall(&patched_code_, patch.LiteralOffset(),
136 offset + patch.LiteralOffset(), target_offset);
137 } else if (patch.Type() == kLinkerPatchDexCacheArray) {
138 uint32_t target_offset = dex_cache_arrays_begin_ + patch.TargetDexCacheElementOffset();
139 patcher_->PatchDexCacheReference(&patched_code_, patch,
140 offset + patch.LiteralOffset(), target_offset);
141 } else {
142 LOG(FATAL) << "Bad patch type.";
143 }
144 }
145 }
146 out_.WriteFully(&code[0], code.size());
147 offset += code.size();
148 }
149 offset = patcher_->WriteThunks(&out_, offset);
150 CHECK_EQ(offset, output_size);
151 CHECK_EQ(output_.size(), output_size);
152 }
153
154 bool CheckLinkedMethod(MethodReference method_ref, const ArrayRef<const uint8_t>& expected_code) {
155 // Sanity check: original code size must match linked_code.size().
156 size_t idx = 0u;
157 for (auto ref : compiled_method_refs_) {
158 if (ref.dex_file == method_ref.dex_file &&
159 ref.dex_method_index == method_ref.dex_method_index) {
160 break;
161 }
162 ++idx;
163 }
164 CHECK_NE(idx, compiled_method_refs_.size());
165 CHECK_EQ(compiled_methods_[idx]->GetQuickCode()->size(), expected_code.size());
166
167 auto result = method_offset_map_.FindMethodOffset(method_ref);
168 CHECK(result.first); // Must have been linked.
169 size_t offset = result.second;
170 CHECK_LT(offset, output_.size());
171 CHECK_LE(offset + expected_code.size(), output_.size());
172 ArrayRef<const uint8_t> linked_code(&output_[offset], expected_code.size());
173 if (linked_code == expected_code) {
174 return true;
175 }
176 // Log failure info.
177 std::ostringstream expected_hex;
178 std::ostringstream linked_hex;
179 std::ostringstream diff_indicator;
180 static const char digits[] = "0123456789abcdef";
181 bool found_diff = false;
182 for (size_t i = 0; i != expected_code.size(); ++i) {
183 expected_hex << " " << digits[expected_code[i] >> 4] << digits[expected_code[i] & 0xf];
184 linked_hex << " " << digits[linked_code[i] >> 4] << digits[linked_code[i] & 0xf];
185 diff_indicator << " ";
186 if (!found_diff) {
187 found_diff = (expected_code[i] != linked_code[i]);
188 diff_indicator << (found_diff ? "^^" : " ");
189 }
190 }
191 CHECK(found_diff);
192 LOG(ERROR) << "diff expected_code linked_code";
193 LOG(ERROR) << "<" << expected_hex.str();
194 LOG(ERROR) << ">" << linked_hex.str();
195 LOG(ERROR) << " " << diff_indicator.str();
196 return false;
197 }
198
199 // Map method reference to assinged offset.
200 // Wrap the map in a class implementing linker::RelativePatcherTargetProvider.
201 class MethodOffsetMap FINAL : public linker::RelativePatcherTargetProvider {
202 public:
203 std::pair<bool, uint32_t> FindMethodOffset(MethodReference ref) OVERRIDE {
204 auto it = map.find(ref);
205 if (it == map.end()) {
206 return std::pair<bool, uint32_t>(false, 0u);
207 } else {
208 return std::pair<bool, uint32_t>(true, it->second);
209 }
210 }
211 SafeMap<MethodReference, uint32_t, MethodReferenceComparator> map;
212 };
213
214 static const uint32_t kTrampolineSize = 4u;
215 static const uint32_t kTrampolineOffset = 0u;
216
217 CompilerOptions compiler_options_;
218 VerificationResults verification_results_;
219 DexFileToMethodInlinerMap inliner_map_;
220 CompilerDriver driver_; // Needed for constructing CompiledMethod.
221 std::string error_msg_;
222 InstructionSet instruction_set_;
223 std::unique_ptr<const InstructionSetFeatures> features_;
224 MethodOffsetMap method_offset_map_;
225 std::unique_ptr<RelativePatcher> patcher_;
226 uint32_t dex_cache_arrays_begin_;
227 std::vector<MethodReference> compiled_method_refs_;
228 std::vector<std::unique_ptr<CompiledMethod>> compiled_methods_;
229 std::vector<uint8_t> patched_code_;
230 std::vector<uint8_t> output_;
231 VectorOutputStream out_;
232};
233
234} // namespace linker
235} // namespace art
236
237#endif // ART_COMPILER_LINKER_RELATIVE_PATCHER_TEST_H_